mysql listing
#1

hi,

i want to list all players with a certain field value but it only picks the first player that has a specific value and then it stops selecting

pawn Код:
if (strcmp("/blackbans", cmdtext, true) == 0)
{
    if(Spieler[playerid][AdminLevel] > 2)
    {

    new query[320];
    format(query, sizeof(query), "SELECT name FROM `Users` WHERE Score > 0");//i want players with this value
    mysql_query(query);
    mysql_store_result();
    mysql_fetch_row(query);
    print(query);//but only 1 player with the value i want is returned

    mysql_free_result();

    }else return SendClientMessage(playerid, 0xFF0000FF, "You don't have permission to use this command!");
    return 1;
}
So how would i make it like it does not stop after the first player but go on untill all players with that value are listed?

thx in advance
Reply
#2

You would need to use the mysql_fetch_row function in a loop, here's an example with your code:

pawn Код:
if (strcmp("/blackbans", cmdtext, true) == 0)
{
    if(Spieler[playerid][AdminLevel] > 2)
    {
        new query[320];
        format(query, sizeof(query), "SELECT name FROM `Users` WHERE Score > 0");//i want players with this value
        mysql_query(query);
        mysql_store_result();
        while(mysql_fetch_row(query))
        {
            print(query);//but only 1 player with the value i want is returned
        }
        mysql_free_result();

    } else return SendClientMessage(playerid, 0xFF0000FF, "You don't have permission to use this command!");
    return 1;
}
Then it will run the loop one time for every row and store the row information in the query variable.

Also note that your query variable definitely does not need to be 320 cells in length!
Reply
#3

mhhh, thx alot for ur fast reply
I could swear ive tried that like that with the loop before and still returned only one name...
Ima try it right now

So the lenght of 24 cells would be enough here? (for the players name?)
Reply
#4

Quote:
Originally Posted by BlackWolf120
Посмотреть сообщение
mhhh, thx alot for ur fast reply
I could swear ive tried that like that with the loop before and still returned only one name...
Ima try it right now

So the lenght of 24 cells would be enough here? (for the players name?)
Well yes, but you need to keep in mind that you need some cells aswell for the format function part of the code!
Reply
#5

thx a lot, it works

Would it also be possible to do it with a dialog?
Like all returned players would be listed in a dialog and not just send via clientmessages or prints?

Just curious if its possible

+rep
Reply
#6

Of course it's possible, just create a string and add the names to it in the loop. Then use the string in a dialog
Reply
#7

pawn Код:
new string[128];//what cell size would u recommend here? It could be about 30 players listed...
format(string, sizeof(string), "%s\n%s",string,query);
ShowPlayerDialog(playerid, 1, DIALOG_STYLE_MSGBOX,"Example_Dialog",string, "Ok", "Close");
Just made this up fast. I dont know exactly how to proceed and i dont want to let my helper go off right now so i post this and i hope u can help me again
Reply
#8

Do that but show player the dialog later (outside the loop). i recomment you use sendclientmessage instead of dialogs too. if you go with dialogs go for [256] bytes
Reply
#9

this is only a small example by me, i know that i have to place the ShowDialog outside the loop.

Thx for ur reply as well but i dont think this will work ordinary, won't it?
Reply
#10

im sorry im running late for school

Код:
CMD:banlist(playerid,params[])
{
	if(admin[playerid] < 2) return scm(playerid,0xFF0000FF,"Your not an admin.");
	new q[128],r;
	format(q,128,"SELECT * FROM ban");
	mysql_query(q);
	mysql_store_result();
	r=mysql_num_rows();
	for(new i=mysql_retrieve_row(1);i<r;i++)
	{
		new field[3][32],name[32],reason[64];
		mysql_fetch_row_format(q,"|");
		explode(q,field,"|");
	    new s[128];
	    format(reason,64,"%s",field[1]);
	    format(name,32,"%s",field[0]);
	    format(s,128,"%s - %s",name,reason);
	    scm(playerid,0xA9C4E4FF,s);
	}
	mysql_free_result();
	return 1;
}
thats how i did. mod it as u wish
Reply
#11

thx.
ahh, u use the mysql plugin by Gizyz(or smth. i really cant remember that name )
I use the one by stricken kid which doesnt support "mysql_fetch_row_format()" but thx alot for ur answer

Now only 1 rep more needed to be able to post in the advertise section

sorry for double post but i just cant get it...


pawn Код:
if (strcmp("/blackbans", cmdtext, true) == 0)
{
    if(Spieler[playerid][AdminLevel] > 3)
    {
        new query[128];
        format(query, sizeof(query), "SELECT name FROM `Users` WHERE BlackBan > 0");
        mysql_query(query);
        mysql_store_result();
        while(mysql_fetch_row(query))
        {
           // It works when i do: print(query);
           ShowPlayerDialog(playerid, 1, DIALOG_STYLE_MSGBOX,"{EE0000}Blacklisted Players",query, "Ok", "Close");
           // But not when i do a dialog like this or a kinda format(); function
        }
        mysql_free_result();

    } else return SendClientMessage(playerid, 0xFF0000FF, "You don't have permission to use this command!");
    return 1;
}
Im getting crazy of it!!
It works great with print, all players with the specific table value are listed.
But why wont it work with anything but print??

Help pls
Reply
#12

pawn Код:
if (strcmp("/blackbans", cmdtext, true) == 0)
{
    if(Spieler[playerid][AdminLevel] > 3)
    {
        new
            query[128],
            results[250];
        format(query, sizeof(query), "SELECT `name` FROM `Users` WHERE `BlackBan` > '0'");
        mysql_query(query);
        mysql_store_result();
        while(mysql_fetch_row(query))
        {
            format(results, sizeof results, "%s%s\r\n", results, query);  
        }
        ShowPlayerDialog(playerid, 1, DIALOG_STYLE_MSGBOX,"{EE0000}Blacklisted Players", results, "Ok", "Close");
        mysql_free_result();
    } else return SendClientMessage(playerid, 0xFF0000FF, "You don't have permission to use this command!");
    return 1;
}
Reply
#13

thx alot it works

But 1 question left:
pawn Код:
format(results, sizeof results, "%s%s\r\n", results, query);  //what does the "r" mean here?
+1rep
Reply
#14

Quote:
Originally Posted by BlackWolf120
Посмотреть сообщение
thx alot it works

But 1 question left:
pawn Код:
format(results, sizeof results, "%s%s\r\n", results, query);  //what does the "r" mean here?
+1rep
The \r is a carriage return and the \n is a newline.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)