[Help] MySQL returning wrong result
#1

I was trying to count number of rows from a table but for some reason I get the wrong result.

I have used this query:
pawn Код:
"SELECT COUNT(*) FROM `"TABLE_INBOX"` WHERE `linkedto` = %d AND `read` = 0"
I ran this query on phpmyadmin and I got the right result there, but not in pawn.
Here is the function, perhaps I used the mysql functions incorrectly?

pawn Код:
stock MySQL_InboxMessages(account_sqlid)
{
    new
        sql_result;
    format(szQuery, sizeof(szQuery), "SELECT COUNT(*) FROM `"TABLE_INBOX"` WHERE `linkedto` = %d AND `read` = 0", account_sqlid);
    mysql_query(szQuery);
    mysql_store_result();
    sql_result = mysql_num_rows();
    mysql_free_result();
    return sql_result;
}
I am using BlueG's MySQL Plugin version R6-2 (non cached queries)

P.S. it is always returning 1
Reply
#2

uhm do mysql_debug(true); at OnGameModeInit or OnFilterScriptInit and check mysql_log.txt ?
Reply
#3

The debug is showing me the same result (1), I've been checking it couple of times

Quote:

>> mysql_query( Connection handle: 1 )

CMySQLHandler::Query(SELECT COUNT(*) FROM `accounts_inbox` WHERE `linkedto` = 1 AND `read` = 0) - Successfully executed.

>> mysql_store_result( Connection handle: 1 )

CMySQLHandler::StoreResult() - Result was stored.

>> mysql_num_rows( Connection handle: 1 )

CMySQLHandler::NumRows() - Returned 1 row(s)

mysql_free_result( Connection handle: 1 )

CMySQLHandler::FreeResult() - Result was successfully free'd.

Reply
#4

It MIGHT be that you use mysql_free_result before you return it. I would try putting "return sql_result" and then freeing the result. I'm not that good with MySQL so I may be completely wrong.
Reply
#5

It has nothing to do with it, I store the result in a variable and only then free the result.
Reply
#6

hmm, i think that COUNT(*) is useless here, just do format(szQuery, sizeof(szQuery), "SELECT COUNT(*) FROM `"TABLE_INBOX"` WHERE `linkedto` = %d AND `read` = 0", account_sqlid);

and return mysql_num_rows();

that should 99% work

pawn Код:
stock MySQL_InboxMessages(account_sqlid)
{
    new
        sql_result;
    format(szQuery, sizeof(szQuery), "SELECT * FROM `"TABLE_INBOX"` WHERE `linkedto` = %d AND `read` = 0", account_sqlid);
    mysql_query(szQuery);
    mysql_store_result();
    sql_result = mysql_num_rows();
    mysql_free_result();
    return sql_result;
}
Reply
#7

Thank you! that worked!

However I can't quite understand why COUNT was not necessary,
would select work similiar if I am not limiting it?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)