AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved A simple query for get total user in Mysql database (https://forums.alliedmods.net/showthread.php?t=323465)

delux450 04-20-2020 10:32

A simple query for get total user in Mysql database
 
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 :)

Nexd 04-20-2020 11:06

Re: A simple query for get total user in Mysql database
 
SQL_GetFieldCount returns the FIELDS not the rows
use SQL_GetRowCount

delux450 04-20-2020 11:19

Re: A simple query for get total user in Mysql database
 
Quote:

Originally Posted by Nexd (Post 2694806)
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);
    
///////////////////////////////////////////////////////////////
    
}



aleeexxx 04-20-2020 14:59

Re: A simple query for get total user in Mysql database
 
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); 


delux450 04-20-2020 19:37

Re: A simple query for get total user in Mysql database
 
Quote:

Originally Posted by aleeexxx (Post 2694878)
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 :shock:

Thanks

aleeexxx 04-20-2020 23:26

Re: A simple query for get total user in Mysql database
 
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

delux450 04-21-2020 06:27

Re: A simple query for get total user in Mysql database
 
Quote:

Originally Posted by aleeexxx (Post 2694978)
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 :wink:

aleeexxx 04-21-2020 11:35

Re: A simple query for get total user in Mysql database
 
Quote:

Originally Posted by delux450 (Post 2695019)
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 :wink:

you're welcome !!


All times are GMT -4. The time now is 21:50.

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