MySQL storing result as a variable
#1

Hello

I am trying to learn how to use MySQL with PAWN and I have this quick inquiry. How would I go about storing the result of a query into a variable, so I could show it in a dialog?

Here's what I got:
(I am trying to show the reason of the ban to the player in a dialog)
pawn Код:
// This is inside an if statement, which explains the indentation.
        new reason[128]; // Variable I would like to store the result in.
        format(query, sizeof(query), "SELECT reason FROM `bans` WHERE IP = '%s'", GetName(playerid)); // Format query (I already got the player's IP)
        mysql_query(query);// Store query
        mysql_store_result();// Store the result
        // And here is where I want to store the result into a variable
        format(ccstring, sizeof(ccstring), "We're sorry but you're IP is banned from the server.\nIf you would like to appeal, please appeal on our website\nat {0320FF}"website"\n\nReason: %s", reason); // Format the dialog
        ShowPlayerDialog(playerid, 10, DIALOG_STYLE_MSGBOX, "{FF0303}[X]{FFFFFF} Banned", ccstring, "Okay", ""); // Show the dialog
        Kick(playerid); // Kick the player because he's banned.
// Rest of code
Reply
#2

pawn Код:
format(query, sizeof(query), "SELECT reason FROM `bans` WHERE IP = '%s'", GetName(playerid));
Don't you mean GetIp(playerid) instead of GetName(playerid)?

The answer to your question: to fetch a value from a row do the following.

pawn Код:
new reason[64]; // Your reason will be stored in this
mysql_query("SELECT reason FROM bans"); // Fetch the reason and store it in a row
mysql_store_result(); // Store the resulting row in the buffer
if(mysql_retrieve_row()) {
    mysql_fetch_field("reason", reason); // Fetch the "reason" field from the stored row, and place it in the reason variable
}
mysql_free_result(); // Free the buffer
printf("The reason is: %s", reason);
Hope that helped. MySQL in pawn can be confusing since it's quite a bit different from (for example) PHP-MySQL.
Reply
#3

Try this:
pawn Код:
new data[128];
format(query, sizeof(query),"SELECT * FROM bans WHERE IP = '%s'",GetName(playerid)); //GetName? It says IP, well...
mysql_query(query), mysql_store_result();
if(mysql_num_rows())
{
    mysql_get_field("reason", data);
    format(ccstring, sizeof(ccstring), "We're sorry but you're IP is banned from the server.\nIf you would like to appeal, please appeal on our website\nat {0320FF}"website"\n\nReason: %s", data); // Format the dialog
    ShowPlayerDialog(playerid, 10, DIALOG_STYLE_MSGBOX, "{FF0303}[X]{FFFFFF} Banned", ccstring, "Okay", ""); // Show the dialog
    Kick(playerid);
}
mysql_free_result();
_____________________________
EDIT:
@Sinner: I think you have a little mistake here:
pawn Код:
mysql_query("SELECT reason FROM bans");
What if there are many fields with more reasons? I mean many fields with different IPs and reasons. How do you know which one is the correct field?
Reply
#4

Quote:
Originally Posted by Sinner
Посмотреть сообщение
pawn Код:
format(query, sizeof(query), "SELECT reason FROM `bans` WHERE IP = '%s'", GetName(playerid));
Don't you mean GetIp(playerid) instead of GetName(playerid)?

The answer to your question: to fetch a value from a row do the following.

pawn Код:
new reason[64]; // Your reason will be stored in this
mysql_query("SELECT reason FROM bans"); // Fetch the reason and store it in a row
mysql_store_result(); // Store the resulting row in the buffer
if(mysql_retrieve_row()) {
    mysql_fetch_field("reason", reason); // Fetch the "reason" field from the stored row, and place it in the reason variable
}
mysql_free_result(); // Free the buffer
printf("The reason is: %s", reason);
Hope that helped. MySQL in pawn can be confusing since it's quite a bit different from (for example) PHP-MySQL.
Ah I didn't catch that, thanks for your help

EDIT:

Unfortunately both of those solutions did not work. I tried to print the reason but it's not printing the reason.
(Yes, the database already has a ban with the reason and IP)
Here's what I have:
pawn Код:
// Code removed
EDIT: (FIXED!)

Ah, nvm it was a dumb mistake xD Thanks for all your help.
Reply
#5

Quote:
Originally Posted by irinel1996
Посмотреть сообщение
Try this:
EDIT:
@Sinner: I think you have a little mistake here:
What if there are many fields with more reasons? I mean many fields with different IPs and reasons. How do you know which one is the correct field?
Yeah it was just an example
"SELECT data FROM field WHERE condition" would be the correct query ofcourse
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)