SQL results.
#1

How can I get results from my SQL database and store them into variables/strings for later use?


I am trying to create a remote-mdc system for my roleplay and I can't figure out how to save the queried items into something which can be used later on in the script?


Thanks!

EG:

pawn Код:
mysql_query("SELECT `Fines` FROM `Accounts` WHERE `Username` = '%s'", NameEntered);
new string[10];
format(string, sizeof(string), "Fines: %d", //this is where I don't understand the variables.
ShowPlayerDialog(playerid, 14214, DIALOG_STYLE_MSGBOX, "MDC Beta", string, "OK", "");
Reply
#2

Use,

mysql_store_result(); and
mysql_fetch_field_row
Reply
#3

Could you please give me an example on how to do it?

Sorry for being a pain in the backside, but once I know how to, I'll keep it in my knowledge :3

Thanks Kitten!
Reply
#4

PHP код:
new string[10];
mysql_query("SELECT `Fines` FROM `Accounts` WHERE `Username` = '%s'"NameEntered);
mysql_store_result(); mysql_retrieve_row();
mysql_fetch_field_row(string"fines");
format(stringsizeof(string), "Fines: %s"string);
ShowPlayerDialog(playerid14214DIALOG_STYLE_MSGBOX"MDC Beta"string"OK""");
mysql_free_result(); 
Reply
#5

pawn Код:
mysql_query("SELECT Username, Fines FROM `Accounts` WHERE `Username` = '%s'", NameEntered);
mysql_store_result( );
if( mysql_num_rows( ) )
{
    new Field[ 10 ];
    mysql_fetch_row_format( query, "|" );
    mysql_fetch_field_row( Field, "Fines" );
   
    format(string, sizeof(string), "Fines: %d", strval( Field ) );
    ShowPlayerDialog(playerid, 14214, DIALOG_STYLE_MSGBOX, "MDC Beta", string, "OK", "");
}
mysql_free_result( );
Reply
#6

Thanks!

I kinda get it, if I have anymore problems, I will post here!

Reputation for the three of you has been given!
Reply
#7

I personally believe you're better off using BlueG's threaded queries. Something along these lines:

pawn Код:
new str[128]; // cbf to count for an example lol
// Remember to actually escape the name, otherwise u'll be vulnerable for SQL Injections
format(str, 128, "SELECT Username, Fines FROM `Accounts` WHERE `Username` = '%s'", NameEntered);
mysql_function_query( connectionHandle, str, true, "StoreMDC", "i", playerid );


forward StoreMDC(playerid);
public StoreMDC(playerid)
{
    new rows, fields;
    cache_get_data(rows, fields);
    if(rows)
    {
        new tmp[2][32];
        cache_get_field_content(0/*row*/, "Username", tmp[0], connectionHandle);
        cache_get_field_content(0, "Fines", tmp[1], connectionHandle);
    }
    return 1;
}
The above is threaded and doesn't pause your script while awaiting a response from the database. I am just gonna assume you know how to convert a string to a value. ( strval *cough* ).

You should also have a look at this very important post related to that system:

https://sampforum.blast.hk/showthread.php?tid=337810

and for security issues that come up with this threaded architecture check this one out:

http://forum.sa-mp.com/showpost.php?...postcount=2141

You'll find the link above at the end of the first thread as well and there will be another one explaining how it all works and how you may retrieve the info. I am sure you'll find your way through =).

Regards.
Reply
#8

You can still use sscanf to store the mysql data.
Reply
#9

Here is an easier query than all of the above answers:

pawn Код:
format(sqlquery,sizeof(sqlquery),"SELECT Fines FROM Accounts WHERE Username='%s'",NameEntered);
mysql_query(sqlquery);
mysql_store_result();
new PFines = mysql_fetch_int();
mysql_free_result(); //Always have mysql_free_result() after a mysql_store_result().
I hope that is easier for you to understand.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)