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

SQL Invalid handle?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
BrUn3S
Member
Join Date: Jan 2012
Location: Slovakia
Old 01-18-2018 , 19:56   SQL Invalid handle?
Reply With Quote #1

Hi guys... I have a issue.. when I want get some data from my db of admins.. It doesn't work but syntax in phpmyadmin working correctly.. please, can someone look on it?
PHP Code:
public bool:GetUserDatabaseData(Handle:dbString:steamID[], client)
{
    if(
IsFakeClient(client)) return false;
    
decl String:query[1024];
    
decl String:flagsAndImmunity[68];
    
decl String:DateExpiration[12];
    
decl String:DaysLeft[12];
    
Format(querysizeof(query), "SELECT srv_flags, DateExpiration, TIMESTAMPDIFF(DAY, NOW(), DATE_FORMAT(STR_TO_DATE(DateExpiration, '%d.%m.%Y'),'%Y-%m-%d %H:%i:%s')) AS days_left FROM a_sb_admins WHERE authid LIKE '%s'"steamID);
    new 
Handle:hQuery SQL_Query(dbquery);
    if (
hQuery != INVALID_HANDLE)
    {
        
PrintToServer("Database request : Hrac %s (%N) -> reading..."steamIDclient);
        while (
SQL_FetchRow(hQuery))
        {
            
SQL_FetchString(hQuery0flagsAndImmunitysizeof(flagsAndImmunity));
            
SQL_FetchString(hQuery1DateExpirationsizeof(DateExpiration));
            
SQL_FetchString(hQuery2DaysLeftsizeof(DaysLeft));
            
            
strcopy(g_iDateExpiration[client], sizeof(DateExpiration), DateExpiration);
            
strcopy(g_iDaysLeft[client], sizeof(DaysLeft), DaysLeft);
            
            
PrintToServer(" %s (%N) '%s' %s",steamIDclientflagsAndImmunityDateExpiration);
        }
        
CloseHandle(hQuery);
        return 
true;
    }
    return 
false;

I don't know what I have wrong with this code..
Thanks.
BrUn3S is offline
Totenfluch
AlliedModders Donor
Join Date: Jan 2012
Location: Germany
Old 01-18-2018 , 20:47   Re: SQL Invalid handle?
Reply With Quote #2

Your whole code structure is flawed. Let me give you a better template:

PHP Code:
public void getUserDatabaseData(int client) {
    
char steamID[20];
    
GetClientAuthId(clientAuthId_Steam2steamIDsizeof(steamID));
    
    
char query[1024];
    
Format(querysizeof(query), "SELECT srv_flags, DateExpiration, TIMESTAMPDIFF(DAY, NOW(), DATE_FORMAT(STR_TO_DATE(DateExpiration, '%d.%m.%Y'),'%Y-%m-%d %H:%i:%s')) AS days_left FROM a_sb_admins WHERE authid = '%s'"steamID); 
    
SQL_TQuery(g_DBgetDataCallbackqueryGetClientUserId(client));
}

public 
void getDatesQueryCallback(Handle ownerHandle hndl, const char[] errorany data) {
    
int client GetClientOfUserId(data);
    
char query[1024]; 
    
char flagsAndImmunity[68]; 
    
char DateExpiration[12]; 
    
char DaysLeft[12]; 
    
    if (
hndl!= INVALID_HANDLE
    { 
        while (
SQL_FetchRow(hndl)) 
        { 
            
SQL_FetchString(hndl0flagsAndImmunitysizeof(flagsAndImmunity)); 
            
SQL_FetchString(hndl1DateExpirationsizeof(DateExpiration)); 
            
SQL_FetchString(hndl2DaysLeftsizeof(DaysLeft)); 
             
            
strcopy(g_iDateExpiration[client], sizeof(DateExpiration), DateExpiration); 
            
strcopy(g_iDaysLeft[client], sizeof(DaysLeft), DaysLeft); 
             
            
PrintToServer(" %s (%N) '%s' %s",steamIDclientflagsAndImmunityDateExpiration); 
        }
    } 

__________________
Notable Projects:
Event Item Spawner | Scissors, Rock, Paper for ZephStore
tVip | Smart Link Remover
PLG & GGC - CS:GO Roleplay

and countless more...

I can make a helicopter shoot missles if you want me to...

Last edited by Totenfluch; 01-18-2018 at 20:48.
Totenfluch is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 01-18-2018 , 21:08   Re: SQL Invalid handle?
Reply With Quote #3

If you look at your error log, you'll see an error about your Format call being broken. At the least, you'll have to escape some of your percent signs so Format doesn't try to handle them all.

Last edited by Fyren; 01-18-2018 at 21:08.
Fyren is offline
BrUn3S
Member
Join Date: Jan 2012
Location: Slovakia
Old 01-19-2018 , 04:16   Re: SQL Invalid handle?
Reply With Quote #4

Thanks for reply..

I was rewrite this, now looks like this
PHP Code:
public void getUserDatabaseData(int client) {
    
char steamID[20];
    
GetClientAuthId(clientAuthId_Steam2steamIDsizeof(steamID));
    
    
char query[1024];
    
Format(querysizeof(query), "SELECT srv_flags, DateExpiration, TIMESTAMPDIFF(DAY, NOW(), DATE_FORMAT(STR_TO_DATE(DateExpiration, '%d.%m.%Y'),'%Y-%m-%d')) AS days_left FROM a_sb_admins WHERE authid = '%s'"steamID); 
    
SQL_TQuery(DatabaseConnectiongetDatesQueryCallbackqueryGetClientUserId(client));
}

public 
void getDatesQueryCallback(Handle ownerHandle hndl, const char[] errorany data) {
    
int client GetClientOfUserId(data);
    
char flagsAndImmunity[68]; 
    
char DateExpiration[12]; 
    
char DaysLeft[12];
    
    
char steamID[20];
    
GetClientAuthId(clientAuthId_Steam2steamIDsizeof(steamID));
    
    if (
hndl != INVALID_HANDLE
    {
        
PrintToServer("Database request : Hrac %s (%N) -> Citanie ..."steamIDclient);
        while (
SQL_FetchRow(hndl)) 
        { 
            
SQL_FetchString(hndl0flagsAndImmunitysizeof(flagsAndImmunity)); 
            
SQL_FetchString(hndl1DateExpirationsizeof(DateExpiration)); 
            
SQL_FetchString(hndl2DaysLeftsizeof(DaysLeft)); 
            
            
strcopy(g_iDateExpiration[client], sizeof(DateExpiration), DateExpiration); 
            
strcopy(g_iDaysLeft[client], sizeof(DaysLeft), DaysLeft); 
        }
    }

still error
PHP Code:
[SMException reportedString formatted incorrectly parameter 5 (total 4
BrUn3S is offline
Totenfluch
AlliedModders Donor
Join Date: Jan 2012
Location: Germany
Old 01-19-2018 , 20:05   Re: SQL Invalid handle?
Reply With Quote #5

PHP Code:
'%d.%m.%Y'),'%Y-%m-%d' 
This gets interpreted as placeholders for variables instead of %..

You have to escape them with \

PHP Code:
\%d  => %d
%=> replacement for a number 
so your code should look like

PHP Code:
Format(querysizeof(query), "SELECT srv_flags, DateExpiration, TIMESTAMPDIFF(DAY, NOW(), DATE_FORMAT(STR_TO_DATE(DateExpiration, '\%d.\%m.\%Y'),'\%Y-\%m-\%d')) AS days_left FROM a_sb_admins WHERE authid = '%s'"steamID); 
(not tested tho)
__________________
Notable Projects:
Event Item Spawner | Scissors, Rock, Paper for ZephStore
tVip | Smart Link Remover
PLG & GGC - CS:GO Roleplay

and countless more...

I can make a helicopter shoot missles if you want me to...
Totenfluch is offline
BrUn3S
Member
Join Date: Jan 2012
Location: Slovakia
Old 01-19-2018 , 20:13   Re: SQL Invalid handle?
Reply With Quote #6

Yea, thanks for help. I resolve this by using timestamp and FormatTime.. thanks a lot
BrUn3S is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 01-21-2018 , 04:39   Re: SQL Invalid handle?
Reply With Quote #7

Quote:
Originally Posted by Totenfluch View Post
You have to escape them with \
That is incorrect, to specify a literal % in a format string you use the %% format specifier.

PHP Code:
DATE_FORMAT(STR_TO_DATE(DateExpiration'%%d.%%m.%%Y'),'%%Y-%%m-%%d'
Also, GetClientAuthId can fail, do not skip checking the return value.
__________________

Last edited by asherkin; 01-21-2018 at 04:40.
asherkin 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 15:45.


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