Raised This Month: $ Target: $400
 0% 

[SOLVED] query not returning results!?


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
SeriousSam
Senior Member
Join Date: Aug 2009
Location: Bulgaria
Old 03-22-2011 , 18:57   Re: query not returning results!?
Reply With Quote #7

If I got it right, instead of calling check_privs each time in the loop, I'll move the conversion switch from privID to letter flags up there and save them in a string, then after the loop I'll call check_privs just once and pass the string as data, and after I get result from the select, if there is no such user I'll add it with the whole string of flags, if he exists, I'll just replace his string of flags with the new one. I think this should fix the problem, but it got late, so I'll try it tomorrow

EDIT:
Here is the code that I got after these changes. I think it looks more readable this way, so it was a good idea to edit it like this. I even added something that I had forgotten to add the first time I haven't yet tested if it works now. I'll check now.

EDIT 2: just checked it, it does work, at least for the first map. When I change the map, the access field gets updated with gibberish. And I have no idea why. Help please!

PHP Code:
#include <amxmodx>
#include <string>
#include <sqlx>

// Plugin information
#define PLUGIN "Miro's Admin System"
#define VERSION "1.1"
#define AUTHOR "SeriousSamBG"

// Connection info
#define HOST "127.0.0.1"
#define USER "secret"
#define PASS "topsecret"
#define DB "miro_cs"

new Handle:g_SqlTuple
new const g_sqlTable_users[] = "users"
new const g_sqlTable_privs[] = "user_privileges"

public plugin_init()
{
    
register_plugin(PLUGINVERSIONAUTHOR);
    
    
sql_init()
}

public 
sql_init()
{
    
g_SqlTuple SQL_MakeDbTuple(HOSTUSERPASSDB)
}

bool:compare_arrays(array1[], array2[], size)
{
   new 
i
   
for (i=0i<sizei++)
   {
      if (
array1[i] != array2[i])
      {
         return 
false
      
}
   }
 
   return 
true
}

public 
user_load(id)
{
    new 
Data[1]
    
Data[0] = id

    
new name[17]
    
get_user_name(idname16)

    new 
cache[512]
    
formatex(cache511"SELECT * FROM %s WHERE UserName='%s';"g_sqlTable_usersname)
    
SQL_ThreadQuery(g_SqlTuple"handle_user"cacheData1)
    return 
PLUGIN_HANDLED
}

public 
handle_user(FailStateHandle:Query,Error[], ErrcodeData[], DataSize)
{
    if(
FailState)
    {
        
log_amx("SQL Error: %s (%d)"ErrorErrcode)
        return 
PLUGIN_HANDLED
    
}
    
    if(
SQL_MoreResults(Query))
    {
        new 
name[17], pass[11], pass_check[11]
        new 
userID SQL_ReadResult(QuerySQL_FieldNameToNum(Query"autoID"))
        
SQL_ReadResult(QuerySQL_FieldNameToNum(Query"UserName"), name16)
        
SQL_ReadResult(QuerySQL_FieldNameToNum(Query"Password"), pass10)
        
        
get_user_info(Data[0], "_pw"pass_check10)
        
        if(
compare_arrays(passpass_check10))
        {
            new 
cache[512]
            
            
formatex(cache511"SELECT * FROM %s WHERE userID='%d';"g_sqlTable_privsuserID)
            
SQL_ThreadQuery(g_SqlTuple"handle_priv"cacheData1)
        }
    }
    else
    {
        
log_amx("retrieved no results ....")
    }

    return 
PLUGIN_HANDLED
}

public 
handle_priv(FailStateHandle:QueryError[], ErrcodeData[], DataSize)
{
    if(
FailState)
    {
        
log_amx("SQL Error: %s (%d)"ErrorErrcode)
        return 
PLUGIN_HANDLED
    
}
    
    if(!
SQL_MoreResults(Query))
    {
        
log_amx("retrieved no results ....")
        return 
PLUGIN_HANDLED
    
}
    
    new 
access[32]
    new 
len formatex(access31Data[0])
    
    do
    {
        new 
privID SQL_ReadResult(QuerySQL_FieldNameToNum(Query"privID"))
        new 
autodate SQL_ReadResult(QuerySQL_FieldNameToNum(Query"autodate"))
        new 
period SQL_ReadResult(QuerySQL_FieldNameToNum(Query"period"))
        new 
final_moment autodate 86400*period
        
        
if(get_systime() > final_momentremove_privs(Data[0], privID)
        else
        {
            switch(
privID)
            {
                case 
1:
                {
                    
len += formatex(access[len], 31-len"a")
                }
                case 
4:
                {
                    
len += formatex(access[len], 31-len"b")
                }
                case 
7:
                {
                    
len += formatex(access[len], 31-len"f")
                }
                case 
10:
                {
                    
len += formatex(access[len], 31-len"j")
                }
                case 
13:
                {
                    if(
containi(access"a") == -1len += formatex(access[len], 31-len"a")
                    if(
containi(access"b") == -1len += formatex(access[len], 31-len"b")
                    if(
containi(access"f") == -1len += formatex(access[len], 31-len"f")
                    if(
containi(access"j") == -1len += formatex(access[len], 31-len"j")
                }
            }
        }
        
        
SQL_NextRow(Query)
    }
    while(
SQL_MoreResults(Query))

    new 
name[17]
    
get_user_name(Data[0], name16)
    
    new 
cache[512]
    
    
formatex(cache511"SELECT * FROM admins WHERE auth='%s' LIMIT 1;"name)
    
SQL_ThreadQuery(g_SqlTuple"handle_check_privs"cacheaccess, ++len)
    
    return 
PLUGIN_HANDLED
}

public 
handle_check_privs(FailStateHandle:QueryError[], ErrcodeData[], DataSize)
{
    if(
FailState)
    {
        
log_amx("SQL Error: %s (%d)"ErrorErrcode)
        return 
PLUGIN_HANDLED
    
}
    
    new 
cache[512]
    new 
access[31]
    new 
name[17]
    new 
pass[11]
    
    
get_user_name(Data[0], name16)
    
get_user_info(Data[0], "_pw"pass10)
    
    
formatex(access30Data[2])
    
    if(
SQL_MoreResults(Query))
    {
        
formatex(cache511"UPDATE admins SET access = '%s' WHERE auth = '%s' LIMIT 1;"accessname)
        
SQL_ThreadQuery(g_SqlTuple"handle_add_privs"cache)
    }
    else
    {    
        
formatex(cache511"INSERT INTO admins VALUES('%s', '%s', '%s', 'k');"namepassaccess)
        
SQL_ThreadQuery(g_SqlTuple"handle_add_privs"cache)
    }

    return 
PLUGIN_HANDLED
}

public 
remove_privs(idprivID)
{
    new 
name[17]
    
get_user_name(idname16)
    
    new 
cache[512]
    
    
formatex(cache511"DELETE FROM admins WHERE auth='%s' LIMIT 1;"name)
    
SQL_ThreadQuery(g_SqlTuple"handle_add_privs"cache)
    
    
formatex(cache511"DELETE FROM user_privileges WHERE privID='%d' LIMIT 1;"privID)
    
SQL_ThreadQuery(g_SqlTuple"handle_add_privs"cache)
    
    
user_load(id)
    return 
PLUGIN_HANDLED
}

public 
handle_add_privs(FailStateHandle:QueryError[], ErrcodeData[], DataSize)
{
    if(
FailState)
    {
        
log_amx("SQL Error: %s (%d)"ErrorErrcode)
        return 
PLUGIN_HANDLED
    
}

    return 
PLUGIN_HANDLED
}

public 
client_putinserver(id)
{
    
user_load(id)
    
    return 
PLUGIN_HANDLED
}

public 
plugin_end()
{
    
SQL_FreeHandle(g_SqlTuple)

__________________

Last edited by SeriousSam; 03-23-2011 at 17:49.
SeriousSam is offline
Send a message via Skype™ to SeriousSam
 



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 14:29.


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