while 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: while in mysql (
/showthread.php?tid=492317)
while in mysql -
Yako - 03.02.2014
Код:
stock UnreadMessages(playerid)
{
new query[256], pname[MAX_PLAYER_NAME], string[256];
GetPlayerName(playerid, pname, sizeof(pname));
format(query, sizeof(query), "SELECT Sender, Message, id FROM `messages` WHERE `Receiver` = '%s' AND `Read` ='0'", pname);
mysql_query(query);
mysql_store_result();
while(mysql_num_rows())
{
mysql_fetch_row(string);
new message[256], sender[MAX_PLAYER_NAME], id;
sscanf(string, "p<|>s[24]s[256]d", sender, message, id);
str_replace(sender, '_', ' ');
format(string, sizeof(string), ""COL_ORANGE"[O-PM] from %s:"COL_YELLOW" %s", sender, message);
SendSplitMessage(playerid, -1, string);
format(query, sizeof(query), "UPDATE `messages` SET `Read` = '1' WHERE id = '%d'", id);
mysql_query(query);
mysql_free_result();
}
return 1;
}
Why this code sends only 1 message?
AW: while in mysql -
selEcT - 03.02.2014
because you can't run a second query in a while loop. Maybe you can do an array with all the id's of the messages which you already read and then make a new query.
edit: or better:
Код:
stock UnreadMessages(playerid)
{
new query[256], query2[256], pname[MAX_PLAYER_NAME], string[256], i=0;
GetPlayerName(playerid, pname, sizeof(pname));
format(query2, sizeof(query2), "UPDATE `messages` SET `Read` = '1' WHERE");
format(query, sizeof(query), "SELECT Sender, Message, id FROM `messages` WHERE `Receiver` = '%s' AND `Read` ='0'", pname);
mysql_query(query);
mysql_store_result();
while(mysql_num_rows())
{
mysql_fetch_row(string);
new message[256], sender[MAX_PLAYER_NAME], id;
sscanf(string, "p<|>s[24]s[256]d", sender, message, id);
str_replace(sender, '_', ' ');
format(string, sizeof(string), ""COL_ORANGE"[O-PM] from %s:"COL_YELLOW" %s", sender, message);
SendSplitMessage(playerid, -1, string);
if(i > 0) //if there are more than 1 message
{
format(query2, sizeof(query2), "%s OR id = %d", query2, id);
}
else
{
format(query2,sizeof(query2), "%s id = %d",query2,id);
}
i++;
}
mysql_query(query2);
mysql_free_result();
return 1;
}