SA-MP Forums Archive
SQL results. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: SQL results. (/showthread.php?tid=348877)



SQL results. - iGetty - 07.06.2012

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", "");



Re: SQL results. - Kitten - 07.06.2012

Use,

mysql_store_result(); and
mysql_fetch_field_row


Re: SQL results. - iGetty - 07.06.2012

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!


Re: SQL results. - mati233 - 07.06.2012

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(); 



Re: SQL results. - Lorenc_ - 07.06.2012

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( );



Re: SQL results. - iGetty - 07.06.2012

Thanks!

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

Reputation for the three of you has been given!


AW: SQL results. - Extremo - 07.06.2012

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.


Re: SQL results. - kikito - 07.06.2012

You can still use sscanf to store the mysql data.


Re: SQL results. - Pizzy - 10.06.2012

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.