MySQL Looping - 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 Looping (
/showthread.php?tid=303094)
MySQL Looping -
SuperViper - 11.12.2011
How would I go through all players and do an action. My current code looks like:
pawn Код:
new string[128], pNm[MAX_PLAYER_NAME], convert[50], var;
mysql_query("SELECT `name` FROM `players` WHERE `var1` > 1 AND `var2` != 1");
mysql_store_result();
while(mysql_fetch_row(pNm)) {
format(string, sizeof(string), "SELECT `var1` FROM `players` WHERE `name`='%s'", playersName);
mysql_query(string);
mysql_store_result();
mysql_fetch_row(convert);
var = strval(convert);
format(string, sizeof(string), "UPDATE `players` SET `var1`='%d' WHERE `name`='%s' AND `var2`!=1", var-45, pNm);
mysql_query(string);
}
But that only does this to the first player it selects.
Re: MySQL Looping -
GamingTurf - 11.12.2011
Instead of "SELECT 'name'"
-> "SELECT *"
Re: MySQL Looping -
SuperViper - 11.12.2011
Then how am I going to get the name?
Re: MySQL Looping -
GamingTurf - 11.12.2011
you store the result, and set a variable to 'name'.. ?
Re: MySQL Looping -
Kingunit - 11.12.2011
You mean you want to get all the information?
pawn Код:
new string[128], pNm[MAX_PLAYER_NAME], convert[50], var;
mysql_query("SELECT * FROM `players` WHERE `var1` > 1 AND `var2` != 1");
mysql_store_result();
while(mysql_fetch_row(pNm)) {
format(string, sizeof(string), "SELECT * FROM `users` WHERE `name`='%s'", playersName);
mysql_query(string);
mysql_store_result();
mysql_fetch_row(convert);
var = strval(convert);
format(string, sizeof(string), "UPDATE `users` SET `var1`='%d' WHERE `name`='%s' AND `var2`!=1", var-45, playersName);
mysql_query(string);
}
Re: MySQL Looping -
SuperViper - 11.12.2011
No, I want to select the 'name' column where var1 is greater than one and var2 doesn't equal 1. I want to then store that result and loop through everyone who matches those circumstances on var1 being greater than once and var2 not being 1 and put their name in the pNm variable. Then I want to get the var1 of the player where the name is equal to pNm and I want to fetch that to the variable convert, and then the variable var is a converted version of the variable convert because I want it to turn out as an integer, not a string. Then I want to send a query to update players and set var1 to var1 minus 45 where the name is pNm and var2 isn't 1.
Re: MySQL Looping -
Hiddos - 11.12.2011
Looking at your code:
pawn Код:
new string[128], pNm[MAX_PLAYER_NAME], convert[50], var;
mysql_query("SELECT `name` FROM `players` WHERE `var1` > 1 AND `var2` != 1");
mysql_store_result(); //Store the result
while(mysql_fetch_row(pNm)) {
format(string, sizeof(string), "SELECT `var1` FROM `players` WHERE `name`='%s'", playersName);
mysql_query(string);
mysql_store_result(); //Store the result over the previous result
mysql_fetch_row(convert);
var = strval(convert);
format(string, sizeof(string), "UPDATE `players` SET `var1`='%d' WHERE `name`='%s' AND `var2`!=1", var-45, pNm);
mysql_query(string);
}
I think your problem is over there.
Re: MySQL Looping -
SuperViper - 11.12.2011
Then how am I supposed to do it?