Can someone help me with a MySQL example - 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: Can someone help me with a MySQL example (
/showthread.php?tid=381499)
Can someone help me with a MySQL example -
Penki4a - 29.09.2012
Ok so i was using the Dini plugin to load/save all of my data.Untill a couple of days ago the owner convinced me to convert the script to MySQL with the purpose of higher performance.I started making my system and all and everything was cool,but i noticed that Blue G changed alot of stuff in the 7th release.So i found AndreT's tutorial on the new R7 functions,but i am having a hard time understanding some of it.Can someone post for me a really short example script with how to save and load one simple string with mySQL R7,because i am having a hard time understanding the new cache functions and the mysql_function_query.If someone can post me an example it will be much apreciated.Thanks,peace
Re: Can someone help me with a MySQL example -
Penki4a - 02.10.2012
Well 48 hours have passed so i might aswell ask does anyone know when the new mysql functions will be on the wiki?
Re: Can someone help me with a MySQL example -
ReneG - 02.10.2012
Wrote this very quickly. Don't forget to escape your strings.
pawn Код:
#define THREAD_LOAD_PLAYER 3
#define THREAD_SAVE_PLAYER 4
new gPass[MAX_PLAYERS][128]; // the string you'll store the password in.
stock LoadPlayer(playerid)
{
new
query[128],
name[24];
GetPlayerName(playerid, name, sizeof(name));
format(query, sizeof(query), "SELECT `password` FROM `users` WHERE `name` = '%s' LIMIT 1", name);
mysql_function_query(connectionHandle, query, true, "OnAccQueryFinish", "dd", playerid, THREAD_LOAD_PLAYER);
return 1;
}
stock SavePlayer(playerid)
{
new
query[128],
name[24];
GetPlayerName(playerid, name, sizeof(name));
format(query, sizeof(query), "UPDATE `users` SET `password` = '%s' WHERE `name` = '%s'", gPass[playerid], name);
mysql_function_query(connectionHandle, query, false, "OnAccQueryFinish", "dd", playerid, THREAD_SAVE_PLAYER); // cache is false since you're saving
return 1;
}
forward OnAccQueryFinish(playerid, threadid);
public OnAccQueryFinish(playerid, threadid)
{
new rows, fields;
cache_get_data(rows, fields, connectionHandle);
switch(threadid)
{
case THREAD_LOAD_PLAYER:
{
if(rows) // if there is rows, then the player is registered
{
cache_get_field_content(row, "password", gPass[playerid], connectionHandle);
// the password is now loaded into gPass
}
else
{
// if not he's a new player
ShowPlayerDialog(...);
}
}
case THREAD_SAVE_PLAYER:
{
SendClientMessage(playerid, -1, "Your account has been saved.");
}
}
return 1;
}
PM me if you ever need any help.