while in mysql
#1

Код:
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?
Reply
#2

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;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)