SA-MP Forums Archive
Inserting random values in MYSQL. - 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: Inserting random values in MYSQL. (/showthread.php?tid=552554)



Inserting random values in MYSQL. - Biess - 25.12.2014

Hello, I have a quick question.
I have a SQL saving system and I have a shop system in which you can buy a cellphone and such.

pawn Код:
PlayerInfo[playerid][pNumber] = randomEx(1234567, 9999999);
format(query, sizeof(query), "UPDATE `users` SET `cellphone` = '1'", GetStringVar(playerid, "Cellphone"), GetName(playerid));
mysql_function_query(dbHandle, query, false, "", "");
Is there a way in MYSQL to add a random value like shown in the code randomEx(1234567, 9999999); as it's needed for my saving system. Thanks in advance.


Re: Inserting random values in MYSQL. - M4D - 25.12.2014

you want to save random number into "cellphone" column ?
what is the name of column that you store "Player nick" into it ?


Re: Inserting random values in MYSQL. - Biess - 25.12.2014

No, I want to save a random number in the 'number' column.


Re: Inserting random values in MYSQL. - M4D - 25.12.2014

so what is your player nick column ?

i don't want to dubble posting so i edited you script just change column names with your own column name

ColumnX ~~~> Player nick column name

pawn Код:
PlayerInfo[playerid][pNumber] = randomEx(1234567, 9999999);
format(query, sizeof(query), "UPDATE `users` SET `number` = %d WHERE `ColumnX` = %s", PlayerInfo[playerid][pNumber], GetName(playerid));
mysql_function_query(dbHandle, query, false, "", "");



Re: Inserting random values in MYSQL. - Biess - 25.12.2014

Thanks for the help I found it out myself, I'll explain..
I have a table called users and the column called number, the column number I wanted to edit.
I made the code like this..
pawn Код:
format(query, sizeof(query), "UPDATE `users` SET `number` = %d", PlayerInfo[playerid][pNumber], GetName(playerid));
                mysql_function_query(dbHandle, query, false, "", "");
And it works.


EDIT: I got one more question, is there a way to add a number to an existing number..
For example I got 2 cigars and I buy one more from the store and it gives me 3 in total..
I have this code.
pawn Код:
PlayerInfo[playerid][pCigars] += 1;
How could I make that into MYSQL, the column name is cigars.


Re: Inserting random values in MYSQL. - M4D - 25.12.2014

Your code is wrong !
You update users table and number column but you have to define WHiCH row you want to update ? All users have number column ! You ha e to put WHERE playername = something (loock at my post above and read query)
Don't send query to database when player buy cigaret
Just store it into a variable and save it on player disconnect !


Re: Inserting random values in MYSQL. - Vince - 25.12.2014

You can do math in MySQL and I honestly prefer saving data intermittently, rather than sending one huge query on disconnect. If the server unexpectedly crashes then all the currently connected players will lose all their stats for that session, whereas with intermittent saves they will only lose a few stats or none at all.

PHP код:
UPDATE character SET cigars cigars 1 WHERE charid = ... 



Re: Inserting random values in MYSQL. - Biess - 25.12.2014

Thanks guys, I managed to fix it.