Hey, I need some SQL assistance!
#1

Howdy,

Well basically after working a lot and trying too hard, I couldn't seem to succeed doing this, so I decided to get a little assistance from you. Yes, I am an experienced scripter but I totally suck with SQL databases, even though I am required to use it to obtain some information I am really in need of to use it in game.

Ok, just before I begin - mysql_connect() is ok with me and everything is fine, I succeed connecting to the database and stuff, but here comes the hard part for me, I do not seem to know how to use mysql_query() - basically, what I want to do is:
  • Connect to the database (completed)
  • Get all user information (including level, etc.)
  • A command /users to display all the users with their levels (one user, one line.)
Yes, I do realize it's newbish to tell you that I am willing to give you a +rep, but 'ey! yes, I am giving it out because I am in need of assistance.

Thanks for your time,
and thanks for reading.
Reply
#2

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!
Reply
#3

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)