Quote:
Originally Posted by JaTochNietDan
mysql_query is well documented around the internet, it's pretty much the same in all languages.
For retrieving some information a table (named users for example), for example all of the columns from a single row specified by the contents of a column called "Username" where the contents of that column are "Skavanovski", you would do something like this:
pawn Код:
mysql_query("SELECT * FROM `users` WHERE Username = 'Skavanovski'");
mysql_store_result(); // Store the result for information gathering
if(mysql_num_rows() > 0) // More than 0 rows were found { new array[200]; // May need to be longer depending on size of your result mysql_fetch_row(array); // Fetch the returned information and store it in the array variable
// The array variable now contains the information in the format: // column1|column2|column3 ... and so on
// Therefore you should use a split function like split or sscanf to split that information into // the necessary variables, for example with sscanf assuming column1 is a string, column2 is an integer // and column3 is a float
new string[30], int, Float: floatvar;
sscanf(array, "s[30]if", string, int, floatvar);
// Now the information inside the array variable has been split into the other variables accordingly. }
If you wanted to select all users and print that information, you would just do this:
pawn Код:
mysql_query("SELECT * FROM `users`"); // Notice we're not specifying a WHERE any more.
mysql_store_result(); // Store the result for information gathering
if(mysql_num_rows() > 0) // More than 0 rows were found { new array[200]; // May need to be longer depending on size of your result while(mysql_fetch_row(array)) // Fetch the returned information and store it in the array variable {
// The array variable now contains the information in the format: // column1|column2|column3 ... and so on
// Therefore you should use a split function like split or sscanf to split that information into // the necessary variables, for example with sscanf assuming column1 is a string, column2 is an integer // and column3 is a float
new string[30], int, Float: floatvar;
sscanf(array, "s[30]if", string, int, floatvar);
// Now the information inside the array variable has been split into the other variables accordingly.
printf("String: %s / Integer: %i / Float: %f", string, int, floatvar); // Print the information for that row } }
Notice how this time we have mysql_fetch_row in a while loop, this means that it will continue executing until it has finished fetching all of the returned rows and storing them in the array variable.
I hope that makes sense!
|
Yes, that's a lot of explanation but exactly what I wanted, yeah, I knew about the single user and how to get the info, but about all users, I didn't know - thanks a bunch, JaTochNietDan. As I promised, a +rep point, thanks for everything.