05.05.2012, 18:11
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:
If you wanted to select all users and print that information, you would just do this:
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!
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.
}
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
}
}
I hope that makes sense!