Problems with mysql result storage
#1

Hello everyone,

I am busy with a gamemode, with some mysql stuff in it, but I get some warnings when I place mysql_free_result on the places that I think are right.
Example:
pawn Код:
stock CheckAccount(username[])
{
    format(query, sizeof(query), "SELECT id FROM players WHERE username='%s'", username);
    mysql_query(query);
    mysql_store_result();
    if (mysql_num_rows() == 1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
    mysql_free_result();
}
Warnings:
pawn Код:
C:\Users\User\Desktop\samp\gamemodes\****.pwn(57) : warning 225: unreachable code
C:\Users\User\Desktop\samp\gamemodes\****.pwn(58) : warning 209: function "CheckAccount" should return a value
Line 57 is the mysql_free_result, and line 58 is ofcourse the } sign.

So, is this a problem for the script? Or can I just ignore it? Can I remove it?

Thanks so far,
Bas
Reply
#2

pawn Код:
stock CheckAccount(username[])
{
    format(query, sizeof(query), "SELECT id FROM players WHERE username='%s'", username);
    mysql_query(query);
    mysql_store_result();
    if (mysql_num_rows() == 1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
    mysql_free_result();  // THIS will never be called!!
   return 1; // THIS fixes the second warning, but wont be called.
}
So maybe try this:

pawn Код:
stock CheckAccount(username[])
{
    format(query, sizeof(query), "SELECT id FROM players WHERE username='%s'", username);
    mysql_query(query);
    mysql_store_result();
    if (mysql_num_rows() == 1)
    {
        mysql_free_result();
        return 1;
    }
    else
    {
        return 0;
    }
}
But I'm not sure, I never worked with MySQL.
Reply
#3

Thanks for your reply. The warnings are gone now, thanks so far. But I'm afraid I'm going to get some errors after I added some more querys. Thanks anyway
Reply
#4

Hehe. Have fun with learning then. ^^
Reply
#5

I see a way to improve this code a little. Why don't you just return the received ID?
pawn Код:
// query and store result
new resultID = strval(mysql_fetch_row());
mysql_free_result();
There are numerous ways to improve the code and "fix" it. Result needs to be freed after using mysql_store_result!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)