mysql counting rows in accounts table
#1

So I just put this to my register action :

PHP Code:
...
new 
registered[24], Cache:aresult mysql_query(mysql"SELECT COUNT(*) FROM `accounts`");
            
cache_get_field_content_int(0registeredmysql);
            
cache_delete(aresult);
            
format(stringsizeof(string), "{FFFF00}%s [%d] has successfully registered. [Total Accounts: %d]"pName(playerid), playeridregistered);
            
SendClientMessageToAll(yellowstring);
... 
But it shows "Player_Name [0] has successfully registered. [Total Accounts: 0]"
Why is it becomes 0? There are 3 rows in the table
Reply
#2

Try this:

pawn Code:
mysql_query(mysql, "SELECT * FROM `accounts`"); // I always use SELECT ID or something else rather than everything (*)

new rows;
cache_get_row_count(rows);

format(string, sizeof(string), "{FFFF00}%s [%d] has successfully registered. [Total Accounts: %d]", pName(playerid), playerid, rows);
Reply
#3

Quote:
Originally Posted by jasperschellekens
View Post
Try this:

pawn Code:
mysql_query(mysql, "SELECT * FROM `accounts`"); // I always use SELECT ID or something else rather than everything (*)

new rows;
cache_get_row_count(rows);

format(string, sizeof(string), "{FFFF00}%s [%d] has successfully registered. [Total Accounts: %d]", pName(playerid), playerid, rows);
It still says that i have 0 players registered. Or should I make it tquery?
Reply
#4

Count begins from 0 as far as I know.
Reply
#5

Code:
cache_get_field_content_int(0, registered, mysql);
pawn Code:
native cache_get_field_content_int(row, const field_name[], connectionHandle = 1);
It expects a column name to search for but `registered` is empty. The correct way would be:
pawn Code:
new Cache:aresult = mysql_query(mysql, "SELECT COUNT(*) AS total_accounts FROM `accounts`"),
    registered = cache_get_field_content_int(0, "total_accounts", mysql);
or
pawn Code:
new Cache:aresult = mysql_query(mysql, "SELECT COUNT(*) FROM `accounts`"),
    registered = cache_get_row_int(0, 0, mysql);
But you do not need a SELECT query at all. When you execute an INSERT query with auto increment, you retrieve the number generated (account ID) with `cache_insert_id`. This is how many players have registered (including deleted accounts).
Reply
#6

Code:
new total = cache_insert_id();
format(string, sizeof(string), "{FFFF00}%s [%d] has successfully registered. [Total Accounts: %d]",pName(playerid), playerid, total);
Reply
#7

Quote:
Originally Posted by Calisthenics
View Post
Code:
cache_get_field_content_int(0, registered, mysql);
pawn Code:
native cache_get_field_content_int(row, const field_name[], connectionHandle = 1);
It expects a column name to search for but `registered` is empty. The correct way would be:
pawn Code:
new Cache:aresult = mysql_query(mysql, "SELECT COUNT(*) AS total_accounts FROM `accounts`"),
    registered = cache_get_field_content_int(0, "total_accounts", mysql);
or
pawn Code:
new Cache:aresult = mysql_query(mysql, "SELECT COUNT(*) FROM `accounts`"),
    registered = cache_get_row_int(0, 0, mysql);
But you do not need a SELECT query at all. When you execute an INSERT query with auto increment, you retrieve the number generated (account ID) with `cache_insert_id`. This is how many players have registered (including deleted accounts).
Thanks.. It's working. I just need to make it "register+1" to include the registered player... REP+
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)