SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 20/05/2020, 08:21 PM   #1
Filbert
Huge Clucker
 
Join Date: May 2016
Posts: 251
Reputation: 1
Default mysql counting rows in accounts table

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
Filbert is offline   Reply With Quote
Old 20/05/2020, 09:09 PM   #2
jasperschellekens
High-roller
 
jasperschellekens's Avatar
 
Join Date: Dec 2016
Location: Holland
Posts: 1,080
Reputation: 126
Default Re: mysql counting rows in accounts table

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);
__________________
If you want to have your own custom script, or even want to have one of our already released scripts, join our SCRIPTING SERVICES DISCORD, or send me a private message!


Undead Wasteland Roleplay - The year is 1996. San Andreas is a wasteland ruled by the undead. the once so glorius landscape of San Andreas, is now a shattered decayed place that is only a vague memory of the world as we used to know it.
Discord - Website
jasperschellekens is offline   Reply With Quote
Old 21/05/2020, 04:44 AM   #3
Filbert
Huge Clucker
 
Join Date: May 2016
Posts: 251
Reputation: 1
Default Re: mysql counting rows in accounts table

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?
Filbert is offline   Reply With Quote
Old 21/05/2020, 05:43 AM   #4
SharpenBlade
Huge Clucker
 
Join Date: May 2020
Posts: 202
Reputation: 25
Default Re: mysql counting rows in accounts table

Count begins from 0 as far as I know.
SharpenBlade is offline   Reply With Quote
Old 21/05/2020, 07:11 AM   #5
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,227
Reputation: 205
Default Re: mysql counting rows in accounts table

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).
__________________
Calisthenics is offline   Reply With Quote
Old 21/05/2020, 07:12 AM   #6
SiaReyes
Gangsta
 
Join Date: Mar 2019
Location: Coordinates X,Y,Z
Posts: 618
Reputation: 57
Default Re: mysql counting rows in accounts table

Code:
new total = cache_insert_id();
format(string, sizeof(string), "{FFFF00}%s [%d] has successfully registered. [Total Accounts: %d]",pName(playerid), playerid, total);
SiaReyes is offline   Reply With Quote
Old 21/05/2020, 09:57 AM   #7
Filbert
Huge Clucker
 
Join Date: May 2016
Posts: 251
Reputation: 1
Default Re: mysql counting rows in accounts table

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+
Filbert is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting something from a MYSQL table PepsiCola23 Scripting Help 4 17/11/2016 12:00 PM
mysql counting rows E7mad Scripting Help 5 03/02/2016 02:44 PM
SELECTING ALL ROWS FROM A TABLE ? - MySQL iReacheR Scripting Help 2 25/06/2014 10:22 AM
How to select only the last 10 rows from table? Bogdan1992 Scripting Help 2 26/03/2012 09:55 AM
[MYSQL]Getting all rows from a table Webghost Scripting Help 7 31/10/2011 05:49 PM


All times are GMT. The time now is 10:01 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.