SA-MP Forums Archive
MySQL Help - 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: MySQL Help (/showthread.php?tid=618168)



MySQL Help - DTV - 02.10.2016

I'm working on a basic register/login system with the latest version of MySQL and ran into an issue with saving string variables. When I register an account, everything appears fine in the database but when I disconnect, all the string variables appear blank.



pawn Код:
//Function that saves all stats on disconnect
public OnAccountSave(playerid)
{
    new Float:x,Float:y,Float:z,Float:a;
    GetPlayerPos(playerid,x,y,z); GetPlayerFacingAngle(playerid,a);
    SaveAccountInt(playerid,"pID",Player[playerid][pID]);
    SaveAccountString(playerid,"pName",Player[playerid][pName]);
    SaveAccountString(playerid,"pPassword",Player[playerid][pPassword]);
    SaveAccountString(playerid,"pSalt",Player[playerid][pSalt]);
        //Other code
        return 1;
}

//SaveAccountString function
public SaveAccountString(playerid,stat[],value[])
{
    new query[128];
    mysql_format(mysql,query,sizeof(query),"UPDATE `accounts` SET `%s` = '%e' WHERE `pID` = '%d'",stat,value,Player[playerid][pID]);
    mysql_tquery(mysql,query,"","");
    return 1;
}
I'm not sure where I'm going wrong here as no errors show up in the logs, so I was wondering if anyone else might know this issue.


Re: MySQL Help - Rdx - 02.10.2016

Why four queries instead of one? It is much more heavier for gamemode. And don't use mysql_tquery if you are not using thread.


Re: MySQL Help - DTV - 02.10.2016

What would I use in place of mysql_tquery then? I've read on the wiki that I shouldn't use mysql_query unless I know what I'm doing and I thought it'd be slower than mysql_tquery.


Re: MySQL Help - Rdx - 02.10.2016

It's not true, lol.
mysql_tquery - threaded queries
mysql_query - standard queries

This is the only difference.


Re: MySQL Help - DTV - 02.10.2016

I can understand why making a query that saves all the variables at once, but how would I do that with over 30-40 variables that needed to be saved? I'll run into an error with the format line being too long and I feel like having a query that's over 128 would be inefficient if there was another way to do it.

EDIT: I also figured out the issue, it had nothing to do with saving but instead not specifying the length of the string variables upon loading an account.


Re: MySQL Help - Rdx - 02.10.2016

Quote:
Originally Posted by DTV
Посмотреть сообщение
I can understand why making a query that saves all the variables at once, but how would I do that with over 30-40 variables that needed to be saved? I'll run into an error with the format line being too long and I feel like having a query that's over 128 would be inefficient if there was another way to do it.
Making a query that saves all at once is just faster.

Example for your question:

Sprintf is my format function.

Код:
new str[400];
strcat(str, sprintf("UPDATE `tablename` SET `float1` = %f, `float2` = %f, `float3` = %f,", x, y, z));
strcat(str, sprintf(" `float4` = %f, `float5` = %f, `float6` = %f WHERE `id` = %d", rx, ry, rz, id));
mysql_query(connectionhandle, str);



Re: MySQL Help - DTV - 02.10.2016

Alright, but I never seen sprintf before, is that a custom function or a native in SA-MP?


Re: MySQL Help - Rdx - 02.10.2016

Код:
new sprintfStr[putyoursize];

#define sprintf(%0,%1) (format(sprintfStr, 1000, %0, %1), sprintfStr)
It's custom. Just for comfort. Very nice for using in queries if you are not using mysql_format (mysql_format should be used only if you need to escape string).