Raised This Month: $32 Target: $400
 8% 

Solved A simple query for get total user in Mysql database


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
delux450
Junior Member
Join Date: Apr 2020
Old 04-20-2020 , 10:32   A simple query for get total user in Mysql database
Reply With Quote #1

Hello Guys,

I begin with sourcepawn and i'm building a script for learning, my plugins is working fine, but now i try to use mysql, i can connecte but after is too hard for me.

I'm searching and reading all about "sourcepawn" and mysql but after many days without success i need help.

I juste want count if a user is existing in my base, like :
PHP Code:
SELECT COUNT(*) FROM transactions 
for after do this :
PHP Code:
if (total == 0) {
do 
this
}
else {
do 
this

i found a litle code in the forum :
https://forums.alliedmods.net/showthread.php?t=262795

This is my code, it work but i got always "1":
PHP Code:
public Event_PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
char error[255];
    
Database db SQL_DefConnect(errorsizeof(error));
 
    if (
db == null)
    {
    
PrintToServer("Could not connect: %s"error);
    } 
    else 
    {
    
///////////////////////////////////////////////////////////////
    
new Handle:db_RowCount SQL_Query(db"SELECT COUNT(*) FROM transactions");
     
    new 
client GetClientOfUserId(GetEventInt(event"userid")); 
    
//
    
new RowCount SQL_GetFieldCount(db_RowCount);
    
    
PrintToChat(client"\x01 \x04## %d ##"RowCount);
    
///////////////////////////////////////////////////////////////
    
}

i tryed the code, but me too i got always "1" on result, someone answer to must use "fetch" for the first row, i understand, but i don't know the "good" syntax for fetch the row...

i'm totally desperat, i'm trying since 4 days night and day (really) for learn, but here i'm lost !

Thanks for all help guys, i do my best i'm sorry but it's new for me

Last edited by delux450; 04-21-2020 at 06:27.
delux450 is offline
Nexd
BANNED
Join Date: Dec 2013
Location: Hungary
Old 04-20-2020 , 11:06   Re: A simple query for get total user in Mysql database
Reply With Quote #2

SQL_GetFieldCount returns the FIELDS not the rows
use SQL_GetRowCount
Nexd is offline
delux450
Junior Member
Join Date: Apr 2020
Old 04-20-2020 , 11:19   Re: A simple query for get total user in Mysql database
Reply With Quote #3

Quote:
Originally Posted by Nexd View Post
SQL_GetFieldCount returns the FIELDS not the rows
use SQL_GetRowCount

Hello Nexd, thanks for your help but i already did it, same result, i got only "1"
I think here i need to make a while/for and count any row ?

this is my code now :
PHP Code:
public Event_PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
    
char error[255];
    
Database db SQL_DefConnect(errorsizeof(error));
 
    if (
db == null)
    {
    
PrintToServer("Could not connect: %s"error);
    } 
    else 
    {
    
///////////////////////////////////////////////////////////////
    
new Handle:db_RowCount SQL_Query(db"SELECT COUNT(*) FROM transactions");
     
    new 
client GetClientOfUserId(GetEventInt(event"userid")); 
    
//
    
new RowCount SQL_GetRowCount(db_RowCount);
    
    
PrintToChat(client"\x01 \x04## %d ##"RowCount);
    
///////////////////////////////////////////////////////////////
    
}

delux450 is offline
aleeexxx
Member
Join Date: May 2014
Location: Valhalla
Old 04-20-2020 , 14:59   Re: A simple query for get total user in Mysql database
Reply With Quote #4

its because you have a single result with count function from SQL, you need to fetch the first column

try with:

PHP Code:
new RowCount SQL_FetchInt(db_RowCount0); 
__________________

Last edited by aleeexxx; 04-20-2020 at 15:18. Reason: index
aleeexxx is offline
Send a message via MSN to aleeexxx
delux450
Junior Member
Join Date: Apr 2020
Old 04-20-2020 , 19:37   Re: A simple query for get total user in Mysql database
Reply With Quote #5

Quote:
Originally Posted by aleeexxx View Post
its because you have a single result with count function from SQL, you need to fetch the first column

try with:

PHP Code:
new RowCount SQL_FetchInt(db_RowCount0); 
Hi aleeexxx thx for your help, i try it but now my plugins don't start in game, nothing happend

May be need somthing more on top of the file ? (DEFINE ? or in plugins start ? lib ? (SDKtools, sdkhook ?))

I really don't understand why is to hard ? in PHP it's easy

Thanks

Last edited by delux450; 04-20-2020 at 19:38.
delux450 is offline
aleeexxx
Member
Join Date: May 2014
Location: Valhalla
Old 04-20-2020 , 23:26   Re: A simple query for get total user in Mysql database
Reply With Quote #6

well its because you need to fetch it with a if or a while


PHP Code:
if(SQL_FetchRow(db_RowCount)) {// you can use while but the if for this case if ok:)
  
new RowCount SQL_FetchInt(db_RowCount0); 
  
PrintToChat(client"\x01 \x04## %d ##"RowCount);

try with this video, maybe can help you: https://www.youtube.com/watch?v=02Kaa_Qv5pk
__________________
aleeexxx is offline
Send a message via MSN to aleeexxx
delux450
Junior Member
Join Date: Apr 2020
Old 04-21-2020 , 06:27   Re: A simple query for get total user in Mysql database
Reply With Quote #7

Quote:
Originally Posted by aleeexxx View Post
well its because you need to fetch it with a if or a while


PHP Code:
if(SQL_FetchRow(db_RowCount)) {// you can use while but the if for this case if ok:)
  
new RowCount SQL_FetchInt(db_RowCount0); 
  
PrintToChat(client"\x01 \x04## %d ##"RowCount);

try with this video, maybe can help you: https://www.youtube.com/watch?v=02Kaa_Qv5pk
Ho aleeexxx really thanks dude it's working very fine now
very very very thanks for all help guys, i hope this post will help others.
i will change to "solved"

Thanks again aleeexxx
delux450 is offline
aleeexxx
Member
Join Date: May 2014
Location: Valhalla
Old 04-21-2020 , 11:35   Re: A simple query for get total user in Mysql database
Reply With Quote #8

Quote:
Originally Posted by delux450 View Post
Ho aleeexxx really thanks dude it's working very fine now
very very very thanks for all help guys, i hope this post will help others.
i will change to "solved"

Thanks again aleeexxx
you're welcome !!
__________________
aleeexxx is offline
Send a message via MSN to aleeexxx
Reply



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

Forum Jump


All times are GMT -4. The time now is 19:07.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode