MySQL Looping
#1

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.
Reply
#2

Instead of "SELECT 'name'"
-> "SELECT *"
Reply
#3

Then how am I going to get the name?
Reply
#4

you store the result, and set a variable to 'name'.. ?
Reply
#5

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);
}
Reply
#6

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.
Reply
#7

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.
Reply
#8

Then how am I supposed to do it?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)