MySQL Query returns null
#1

Hey!,

Somehow, the following returns always a big fat zero!

Code:
        new string[24+MAX_PLAYER_NAME];
		new rows = cache_get_row_count();
		format(string, sizeof(string), "Number of accounts: %d",rows);
    	SendClientMessageToAll(0xC4C4C4FF, string);
Don't go bonkers, I'm new to MySQL. I'm learning on the way.

Thanks,
Cheers.
Reply
#2

If you use MySQL R40; this one is false:
pawn Code:
new rows = cache_get_row_count();
Because cache_get_row_count return 1 on success, 0 on failure. You need to do like this:
pawn Code:
new rows;
cache_get_row_count(rows);
Check this: https://sampwiki.blast.hk/wiki/MySQL/R40..._get_row_count
(That's what Konstantinos teached to me :3)
Reply
#3

Hey!,

The only that bothers me is that there is no MySQL Query or Select FROM type of thing behind that piece of code, while it is being uploaded to my VPS, so I could test it. How is this piece of code going to figure which table I'm talking about? I'm wondering would this code be capable of telling me how many total registered accounts there are on my db? Would this be the most efficient way?

Thanks,
Reply
#4

You would execute a SELECT query before you attempt that function. (Save w cache or do a callback func)
Reply
#5

Hey.
This is from the Wiki:
pawn Code:
new Cache:result = mysql_query(MySQL, "SELECT `id` FROM `players`");
printf("There are %d players in the database.", cache_num_rows());
cache_delete(result);
Reply
#6

If you just want to find out how many registered players you have just use some Primary Auto Increment key inside the Users table or whatever table you want to have, and then you can simply do this to retrieve total players


pawn Code:
SELECT ID FROM users ORDER BY ID DESC LIMIT 1
That would simply select the last inserted ID from table users (Replace ID with your auto-increment variable)

EDIT: Why is this good? It selects only the last ID inserted, so you dont have to load all users and then count them down. That is highly recommended for any sort of tables. Imagine what would happen if you had 100.000+ players in your DB and every time you are running a query to select all of them
Reply
#7

Never use num_rows if you merely want a count of something. Use the COUNT() aggregate function instead. Why? In the former case the entire result set is sent. Possibly a few thousand rows. Not efficient. In the latter case the result is always one single field that contains the count.
Reply
#8

So,
I runned the following MySQL Query on PHPmyAdmin, and it returns what it should return.



But when I do the same on my script, it still returns an ugly 0.
Code:
  		new string[126];
		new Cache:result = mysql_query(handle, "SELECT COUNT(*) FROM `accounts`");
		format(string, sizeof(string), "Number of accounts: %d",cache_get_row_int(0, 0));
		cache_delete(result);
    	SendClientMessageToAll(0xC4C4C4FF, string);
Reply
#9

Have you checked your mysql logs?
Reply
#10

This is spooky.
Code:
[10:57:51] [WARNING] cache_get_row_count - no active cache
[05:42:04] [ERROR] mysql_query - invalid connection handle (id: 0)
[05:42:04] [WARNING] cache_get_row_count - no active cache
[05:42:04] [WARNING] CMySQLHandle::DeleteSavedResult - invalid result id ('0')
[05:50:30] [ERROR] mysql_query - invalid connection handle (id: 0)
[05:50:30] [WARNING] cache_get_row_int - no active cache
[05:50:30] [WARNING] CMySQLHandle::DeleteSavedResult - invalid result id ('0')
[05:52:25] [ERROR] mysql_query - invalid connection handle (id: 0)
[05:52:25] [WARNING] cache_get_row_int - no active cache
[05:52:25] [WARNING] CMySQLHandle::DeleteSavedResult - invalid result id ('0')
[06:10:39] [ERROR] mysql_query - invalid connection handle (id: 0)
[06:10:39] [WARNING] CMySQLHandle::DeleteSavedResult - invalid result id ('0')
[06:22:14] [ERROR] mysql_query - invalid connection handle (id: 0)
[06:22:14] [WARNING] cache_get_row_int - no active cache
[06:22:14] [WARNING] CMySQLHandle::DeleteSavedResult - invalid result id ('0')
[06:53:23] [ERROR] mysql_query - invalid connection handle (id: 0)
[06:53:23] [WARNING] cache_get_row_int - no active cache
[06:53:23] [WARNING] CMySQLHandle::DeleteSavedResult - invalid result id ('0')
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)