SA-MP Forums Archive
Getting all the results from this MySQL Query? - 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: Getting all the results from this MySQL Query? (/showthread.php?tid=324046)



Getting all the results from this MySQL Query? - Dokins - 07.03.2012

This doesnt work, but what I'm trying to do is...

type: /findnumber Grant.

It should check the mysql for all results in that field with the name "Grant" but display the full name.
i.e I search "Grant" and it shows me all the names that begin with or end with Grant.

like
Grant_McCaw
Grant_Johnson
Adrian_Grant.

Etc.

pawn Код:
CMD:findnumber(playerid, params[])
{
    if(LoggedIn[playerid] == 0) return SendClientMessage(playerid, COLOUR_GREY, "You must be logged in to use this command.");
    if(Phonebook[playerid] == 0) return SendClientMessage(playerid, COLOUR_GREY, "You do not have a phonebook, you can buy one from a store.");
    new player[24],string[128], query[256], result[24];
    if(sscanf(params, "s[24]",player)) return SendClientMessage(playerid, COLOUR_GREY, "Usage: /findnumber [player name]");
   
   
    format(query, sizeof(query), "SELECT `Number` FROM `accounts` WHERE `PlayerName` LIKE '%s%%%'", player);
    mysql_query(query); // No query line
    mysql_store_result(); // "Error nothing to store"
    new rows = mysql_num_rows();
    while(mysql_retrieve_row()) //this will be running until all rows are processed
    {
        mysql_fetch_field(rows, result);
    }
    new msnum = mysql_fetch_int();
    if(rows > 0)
        {
            format(string, sizeof(string), "%s - %d",result, msnum);
            SendClientMessage(playerid, COLOUR_WHITE, string);
        }
    if(rows == 0)
        {
            SendClientMessage(playerid, COLOUR_GREY, "There was no numbers found with that name.");
        }
        mysql_free_result();
    return 1;
}



Re: Getting all the results from this MySQL Query? - Vince - 07.03.2012

If you want the part of the name to occur anywhere in the string, then the correct format is '%%%s%%'. Also, you're using mysql_fetch_field wrong. The first parameter is the field index, which - in your case - should always be 0 for the first field (a.k.a. column).


Re: Getting all the results from this MySQL Query? - Dokins - 07.03.2012

So that should return all results?, I've edited it accordingly.


Re: Getting all the results from this MySQL Query? - Dokins - 07.03.2012

This Crashes the server...

pawn Код:
CMD:findnumber(playerid, params[])
{
    if(LoggedIn[playerid] == 0) return SendClientMessage(playerid, COLOUR_GREY, "You must be logged in to use this command.");
    if(Phonebook[playerid] == 0) return SendClientMessage(playerid, COLOUR_GREY, "You do not have a phonebook, you can buy one from a store.");
    new player[24],string[128], query[256], result[24];
    if(sscanf(params, "s[24]",player)) return SendClientMessage(playerid, COLOUR_GREY, "Usage: /findnumber [player name]");
   
   
    format(query, sizeof(query), "SELECT `Number` FROM `accounts` WHERE `PlayerName` LIKE '%%%s%%'", player);
    mysql_query(query); // No query line
    mysql_store_result(); // "Error nothing to store"
    new rows = mysql_num_rows();
    while(mysql_retrieve_row()) //this will be running until all rows are processed
    {
        mysql_fetch_field(1, result);
    }
    new msnum = mysql_fetch_int();
    if(rows > 0)
        {
            format(string, sizeof(string), "%s - %d",result, msnum);
            SendClientMessage(playerid, COLOUR_WHITE, string);
        }
    if(rows == 0)
        {
            SendClientMessage(playerid, COLOUR_GREY, "There was no numbers found with that name.");
        }
        mysql_free_result();
    return 1;
}



Re: Getting all the results from this MySQL Query? - Dokins - 07.03.2012

Bump, I don't know why this isn't working.


Re: Getting all the results from this MySQL Query? - Vince - 07.03.2012

Quote:
Originally Posted by Vince
Посмотреть сообщение
The first parameter is the field index, which - in your case - should always be 0 for the first field (a.k.a. column).
Also post the mysql_log.


Re: Getting all the results from this MySQL Query? - Dokins - 07.03.2012

I changed it to 0, it didn't make a difference, I start all tables at 1 to avoid self confusion. Here it is: (Hang on, Volt-Host is Lagging.

pawn Код:
[23:48:55] CMySQLHandler::Query(SELECT `Number` FROM `accounts` WHERE `PlayerName` LIKE '%Grant%') - Successfully executed.
[23:48:55] >> mysql_store_result( Connection handle: 1 )
[23:48:55] CMySQLHandler::StoreResult() - Result was stored.
[23:48:55] >> mysql_num_rows( Connection handle: 1 )
[23:48:55] CMySQLHandler::NumRows() - Returned 2 row(s)
[23:48:55] >> mysql_retrieve_row( Connection handle: 1 )
[23:48:55] >> mysql_fetch_field( Connection handle: 1 )



Re: Getting all the results from this MySQL Query? - Dokins - 07.03.2012

Updated.


Re: Getting all the results from this MySQL Query? - Dokins - 07.03.2012

Bump.


Re: Getting all the results from this MySQL Query? - Dokins - 08.03.2012

Can someone please respond..