Raised This Month: $51 Target: $400
 12% 

Solved SQL_FetchRow(hndl) problem.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
DarkLightMC
Junior Member
Join Date: Aug 2017
Old 02-24-2018 , 11:42   SQL_FetchRow(hndl) problem.
Reply With Quote #1

I was trying to make a plugin that uses MySQL, but when I was testing it I ran into an error with SQL_FetchRow(hndl). It keeps giving me an error but the error string contains nothing in it. https://gyazo.com/02f0a63f5b5fc7b79b1a8011d8bba710

PHP Code:
#include <sourcemod>

new Handle:db INVALID_HANDLE;
new 
String:steamid[48];
new 
userid;

public 
OnPluginStart()
{
    
LoadTranslations("common.phrases")
    
    
SQL_TConnect(callback"joinpunishments");
    
    
RegAdminCmd("sm_punish"command_punishADMFLAG_BAN);
}

public 
callback(Handle:ownerHandle:hndl, const String:error[], any:data)
{
    
db hndl;

    if(
hndl != INVALID_HANDLE)
    {
        
PrintToServer("JOINPUNISHMENTS connection successful.");
    }
    else
    {
        
PrintToServer("JOINPUNISHMENTS connection error. ERROR: %s"error);
    }
}

public 
Action command_punish(clientargs)
{
    if(
args 4)
    {
        
PrintToChat(client"[SM] Usage: sm_punish <steamid> <punishment, ban/gag/mute> <time> <reason>. The reason must be written with underscores, it wont read spaces(for example: mic_spam_and_chat_spam).");
        
        return 
Plugin_Handled;
    }
    
    new 
String:name[48];
    
GetClientName(clientnamesizeof(name));
    
    new 
String:arg_string[256];
    new 
String:authid[128];
    new 
String:punishment[128];
    new 
String:time[128];
    new 
String:reason[128];

    
int lentotal_len;
    
    
GetCmdArgString(arg_stringsizeof(arg_string));
    
    if ((
len BreakString(arg_string[total_len], authidsizeof(authid))) != -1)
    {
        
total_len += len;
    }
    if ((
len BreakString(arg_string[total_len], punishmentsizeof(punishment))) != -1)
    {
        
total_len += len;
    }
    if ((
len BreakString(arg_string[total_len], timesizeof(time))) != -1)
    {
        
total_len += len;
    }
    if ((
len BreakString(arg_string[total_len], reasonsizeof(reason))) != -1)
    {
        
total_len += len;
    }
    
    
bool idValid false;
    if (!
strncmp(authid"STEAM_"6) && authid[7] == ':')
        
idValid true;
        
    if (!
idValid)
    {
        
ReplyToCommand(client"[SM] Invalid SteamID.");
        return 
Plugin_Handled;
    }
        
    if(
StrContains(punishment"ban"false) == -&& StrContains(punishment"gag"false) == -&& StrContains(punishment"mute"false) == -1)
    {
        
PrintToChat(client"[SM] Invalid punishment. Only the following punishments can be used: ban/gag/mute");
        
        return 
Plugin_Handled;
    }
    
    new 
String:query[200];
    
Format(query200"INSERT INTO list (steamid, punishment, time, reason) VALUES ('%s', '%s', '%s', '%s')"authidpunishmenttimereason);
    new 
Handle:SQLQuery SQL_Query(dbquery);
    
    if(
SQLQuery != INVALID_HANDLE)
    {
        
PrintToChat(client"[SM] User succesfully added to database. If you want to remove that user, please contact the owner.");
        
LogToFile("addons/sourcemod/logs/joinpunishments/punishments.txt""Admin %s added info to DB. Steamid: %s, punishment: %s, time: %s, reason: %s."nameauthidpunishmenttimereason);
    }
    else
    {
        new 
String:Error[255];
        
SQL_GetError(dbErrorsizeof(Error));
        
PrintToChat(client"[SM] Unable to ban client, contact the owner if this happened. ERROR: %s"Error);
    }
            
    return 
Plugin_Handled;
}

public 
OnClientAuthorized(client)
{
    
GetClientAuthString(clientsteamidsizeof(steamid));
    
userid GetClientUserId(client);
    
    new 
String:query[200];
    
    
Format(querysizeof(query), "SELECT steamid, punishment, time, reason FROM list WHERE steamid='%s'"steamid);
    
    
SQL_TQuery(dbQueryCBquery);    

}

public 
QueryCB(Handle:ownerHandle:hndl, const String:error[], any:data)
{
    if(
hndl != INVALID_HANDLE && SQL_FetchRow(hndl))
    {
            new 
String:reason[128];
            new 
String:time[128];
            new 
String:punishment[128];


            
SQL_FetchString(hndl1punishmentsizeof(punishment));
            
SQL_FetchString(hndl2timesizeof(time));
            
SQL_FetchString(hndl3reasonsizeof(reason));

            if(
StrContains(punishment"ban"false) != -1)
            {
                
ServerCommand("sm_ban #%s %s %s"useridtimereason);
                
PrintToServer("ban");
            }

            if(
StrContains(punishment"gag"false) != -1)
            {
                
ServerCommand("sm_gag #%s %s %s"useridtimereason);
                
PrintToServer("gag");
            }

            if(
StrContains(punishment"mute"false) != -1)
            {
                
ServerCommand("sm_mute #%s %s %s"useridtimereason);
                
PrintToServer("mute");
            }
    }
    else
    {
        
PrintToServer("JOINPUNISHMENTS query/handle error. ERROR: %s"error);
    }


Last edited by DarkLightMC; 03-06-2018 at 13:56.
DarkLightMC is offline
xerox8521
Senior Member
Join Date: Sep 2011
Old 02-24-2018 , 12:46   Re: SQL_FetchRow(hndl) problem.
Reply With Quote #2

It does actually tell you the error: "Table 'sourcebans.sb_bans' doesn't exist".

Some advices:
I recommend you to use transitional syntax.

You don't need to save the name in a variable to print it out you can just use the %N specifier.

You don't need to manually extract all the arguments from the command arguements. (GetCmdArg starting by 1)

You save time as %s (also passing userid as string) where it should be %d or %i for integers Format specifiers

You are not escaping input texts or names before inserting them into the database which allows SQL Injection. (SQL_EscapeString)

You shouldn't hardcode the path to sourcemod use BuildPath instead.

userid shouldn't be global imagine a new player joins the server before the database returns the result it would get executed on the wrong player. You can pass data (either directly or using DataPacks) to ther query function.

Last edited by xerox8521; 02-24-2018 at 12:51.
xerox8521 is offline
DarkLightMC
Junior Member
Join Date: Aug 2017
Old 02-24-2018 , 14:42   Re: SQL_FetchRow(hndl) problem.
Reply With Quote #3

Quote:
Originally Posted by xerox8521 View Post
It does actually tell you the error: "Table 'sourcebans.sb_bans' doesn't exist".

Some advices:
I recommend you to use transitional syntax.

You don't need to save the name in a variable to print it out you can just use the %N specifier.

You don't need to manually extract all the arguments from the command arguements. (GetCmdArg starting by 1)

You save time as %s (also passing userid as string) where it should be %d or %i for integers Format specifiers

You are not escaping input texts or names before inserting them into the database which allows SQL Injection. (SQL_EscapeString)

You shouldn't hardcode the path to sourcemod use BuildPath instead.

userid shouldn't be global imagine a new player joins the server before the database returns the result it would get executed on the wrong player. You can pass data (either directly or using DataPacks) to ther query function.
Thank you for the advice. After adding the required tables to 'sourcebans' it still didn't fix the issue: https://gyazo.com/9f4843232abe6e5a4662c14233eeb662.
DarkLightMC is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 02-24-2018 , 16:29   Re: SQL_FetchRow(hndl) problem.
Reply With Quote #4

Could you show us your up-to-date code. I did a Ctrl+F for "query error" and nothing came up.
hmmmmm is offline
DarkLightMC
Junior Member
Join Date: Aug 2017
Old 02-25-2018 , 02:09   Re: SQL_FetchRow(hndl) problem.
Reply With Quote #5

Quote:
Originally Posted by hmmmmm View Post
Could you show us your up-to-date code. I did a Ctrl+F for "query error" and nothing came up.
PHP Code:
#include <sourcemod>

new Handle:db INVALID_HANDLE;

public 
OnPluginStart()
{
    
LoadTranslations("common.phrases")
    
    
SQL_TConnect(callback"joinpunishments");
    
    
RegAdminCmd("sm_punish"command_punishADMFLAG_BAN);
    
    new 
String:path[128];
    
BuildPath(Path_SMpathsizeof(path), "logs/joinpunishments");
    if(!
DirExists(path))
    {
        
CreateDirectory(path511);
        
PrintToServer("[JOINPUNISHMENTS] Directory created.");
    }
    
    
BuildPath(Path_SMpathsizeof(path), "logs/joinpunishments/punishments.txt");
    if(!
FileExists(path))
    {
        
OpenFile(path"a+");
        
PrintToServer("[JOINPUNISHMENTS] Log file created.");
    }
    
}

public 
callback(Handle:ownerHandle:hndl, const String:error[], any:data)
{
    
db hndl;

    if(
hndl != INVALID_HANDLE)
    {
        
PrintToServer("JOINPUNISHMENTS connection successful.");
    }
    else
    {
        
PrintToServer("JOINPUNISHMENTS connection error. ERROR: %s"error);
    }
}

public 
Action command_punish(clientargs)
{
    if(
args 4)
    {
        
PrintToChat(client"[SM] Usage: sm_punish <steamid> <punishment, ban/gag/mute> <time> <reason>. The reason must be written with underscores, it wont read spaces(for example: mic_spam_and_chat_spam).");
        
        return 
Plugin_Handled;
    }
    
    new 
String:arg_string[256];
    new 
String:authid[128];
    new 
String:punishment[128];
    new 
String:time[128];
    new 
String:reason[128];

    
int lentotal_len;
    
    
GetCmdArgString(arg_stringsizeof(arg_string));
    
    if ((
len BreakString(arg_string[total_len], authidsizeof(authid))) != -1)
    {
        
total_len += len;
    }
    if ((
len BreakString(arg_string[total_len], punishmentsizeof(punishment))) != -1)
    {
        
total_len += len;
    }
    if ((
len BreakString(arg_string[total_len], timesizeof(time))) != -1)
    {
        
total_len += len;
    }
    if ((
len BreakString(arg_string[total_len], reasonsizeof(reason))) != -1)
    {
        
total_len += len;
    }
    
    
bool idValid false;
    if (!
strncmp(authid"STEAM_"6) && authid[7] == ':')
        
idValid true;
        
    if (!
idValid)
    {
        
ReplyToCommand(client"[SM] Invalid SteamID.");
        return 
Plugin_Handled;
    }
        
    if(
StrContains(punishment"ban"false) == -&& StrContains(punishment"gag"false) == -&& StrContains(punishment"mute"false) == -1)
    {
        
PrintToChat(client"[SM] Invalid punishment. Only the following punishments can be used: ban/gag/mute");
        
        return 
Plugin_Handled;
    }
    
    
decl String:authid2[128];
    
decl String:punishment2[128];
    
decl String:time2[128];
    
decl String:reason2[128];
    
    
SQL_EscapeString(dbauthidauthid2sizeof(authid2));
    
SQL_EscapeString(dbpunishmentpunishment2sizeof(punishment2));
    
SQL_EscapeString(dbtimetime2sizeof(time2));
    
SQL_EscapeString(dbreasonreason2sizeof(reason2));
    
    new 
String:query[200];
    
Format(query200"INSERT INTO list (steamid, punishment, time, reason) VALUES ('%s', '%s', '%s', '%s')"authid2punishment2time2reason2);
    new 
Handle:SQLQuery SQL_Query(dbquery);
    
    if(
SQLQuery != INVALID_HANDLE)
    {
        
PrintToChat(client"[SM] User succesfully added to database. If you want to remove that user, please contact the owner.");
        
LogToFile("addons/sourcemod/logs/joinpunishments/punishments.txt""Admin %N added info to DB. Steamid: %s, punishment: %s, time: %s, reason: %s."clientauthidpunishmenttimereason);
    }
    else
    {
        new 
String:Error[255];
        
SQL_GetError(dbErrorsizeof(Error));
        
PrintToChat(client"[SM] Unable to ban client, contact the owner if this happened. ERROR: %s"Error);
    }
            
    return 
Plugin_Handled;
}

public 
OnClientAuthorized(client)
{
    new 
String:steamid[48];
    
GetClientAuthString(clientsteamidsizeof(steamid));
    
int id GetClientUserId(client);
    
    new 
String:query[200];
    
    
decl String:steamid2[48];
    
SQL_EscapeString(dbsteamidsteamid2sizeof(steamid2));
    
    
Format(querysizeof(query), "SELECT steamid, punishment, time, reason FROM list WHERE steamid='%s'"steamid2);
    
    new 
Handle:hndl SQL_Query(dbquery);    

    if(
hndl != INVALID_HANDLE && SQL_FetchRow(hndl))
    {
        new 
String:reason[128];
        new 
String:time[128];
        new 
String:punishment[128];
        
        
SQL_FetchString(hndl1punishmentsizeof(punishment));
        
SQL_FetchString(hndl2timesizeof(time));
        
SQL_FetchString(hndl3reasonsizeof(reason));

        if(
StrContains(punishment"ban"false) != -1)
        {
            
ServerCommand("sm_ban #%s %s %s"idtimereason);
            
PrintToServer("ban");
        }

        if(
StrContains(punishment"gag"false) != -1)
        {
            
ServerCommand("sm_gag #%s %s %s"idtimereason);
            
PrintToServer("gag");
        }

        if(
StrContains(punishment"mute"false) != -1)
        {
            
ServerCommand("sm_mute #%s %s %s"idtimereason);
            
PrintToServer("mute");
        }
    }
    else
    {
        new 
String:error[255];
        
SQL_GetError(dberrorsizeof(error));
        
PrintToServer("JOINPUNISHMENTS query error. ERROR: %s"error);
    }


Last edited by DarkLightMC; 02-25-2018 at 02:37.
DarkLightMC is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 02-25-2018 , 03:15   Re: SQL_FetchRow(hndl) problem.
Reply With Quote #6

Are you sure that this query should be returning any rows? SQL_FetchRow will return false if there aren't any rows but that doesn't mean theres an error, and the fact that the error message is blank makes me think that the database loaded just fine.
hmmmmm is offline
DarkLightMC
Junior Member
Join Date: Aug 2017
Old 02-26-2018 , 13:42   Re: SQL_FetchRow(hndl) problem.
Reply With Quote #7

This is how the inside of my table looks like: https://gyazo.com/7930d05bd7650d6f5333e41962bcbff7
https://gyazo.com/622f4c603be00bd9d6d816a158b52a1c
DarkLightMC is offline
Squallkins
AlliedModders Donor
Join Date: Mar 2005
Old 03-01-2018 , 05:16   Re: SQL_FetchRow(hndl) problem.
Reply With Quote #8

Are you testing on a LAN server? steamid2 would be STEAM_ID_LAN if so.
Squallkins is offline
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 09:55.


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