Raised This Month: $ Target: $400
 0% 

[SOLVED] query not returning results!?


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
SeriousSam
Senior Member
Join Date: Aug 2009
Location: Bulgaria
Old 03-22-2011 , 15:14   [SOLVED] query not returning results!?
Reply With Quote #1

Hello everyone! I have a small problem and I need help.

I'm writing this plugin on a request by a friend of mines. It is supposed to read user privileges from a mysql database and enter them into the admins table from the admin_sql plugin. The plugin should also remove privileges that have expired. The mysql database is filled by a website that I also made, each for a limited period, hence they expire

When I tested the plugin, it seems to work for the most part, but it makes a new entry into the admins table for each flag that I want to add. It is supposed to add them to existing users with the update query, but it doesn't. I suspect the query in check_privs function doesn't return results, though looking at the code I can't find a reason for that. Anyway, here is the code:

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

// Plugin information
#define PLUGIN "Miro's Admin System"
#define VERSION "1.0"
#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]
        
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_privsData[0])
            
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
    
}
    
    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])
        else 
check_privs(Data[0], privID)
        
        
SQL_NextRow(Query)
    }
    while(
SQL_MoreResults(Query))

    return 
PLUGIN_HANDLED
}

public 
check_privs(idprivID)
{
    new 
Data[2]
    
Data[0] = id
    Data
[1] = privID

    
new name[17]
    
get_user_name(idname16)
    
    new 
cache[512]
    
    
formatex(cache511"SELECT * FROM admins WHERE auth='%s' LIMIT 1;"name)
    
SQL_ThreadQuery(g_SqlTuple"handle_check_privs"cacheData2)
    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[32]
    new 
name[17]
    new 
pass[11]
    new 
len
    
    get_user_name
(Data[0], name16)
    
get_user_info(Data[0], "_pw"pass10)
    
    if(
SQL_MoreResults(Query))
    {
        
SQL_ReadResult(QuerySQL_FieldNameToNum(Query"access"), access31)
        
        
len format(access31"%s"access)
        
        switch(
Data[1])
        {
            case 
1:
            {
                if(
containi(access"a") == -1len += formatex(access[len], 31-len"a")
            }
            case 
4:
            {
                if(
containi(access"b") == -1len += formatex(access[len], 31-len"b")
            }
            case 
7:
            {
                if(
containi(access"f") == -1len += formatex(access[len], 31-len"f")
            }
            case 
10:
            {
                if(
containi(access"j") == -1len += 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")
            }
            default:
            {
                return 
PLUGIN_HANDLED
            
}
        }
        
        
formatex(cache511"UPDATE admins SET access = '%s' WHERE auth = '%s' LIMIT 1;"accessname)
        
SQL_ThreadQuery(g_SqlTuple"handle_add_privs"cache)
    }
    else
    {    
        switch(
Data[1])
        {
            case 
1:
            {
                
access[0] = 'a'
            
}
            case 
4:
            {
                
access[0] = 'b'
            
}
            case 
7:
            {
                
access[0] = 'f'
            
}
            case 
10:
            {
                
access[0] = 'j'
            
}
            case 
13:
            {
                
access[0] = 'a'
                
access[1] = 'b'
                
access[2] = 'f'
                
access[3] = 'j'
            
}
            default:
            {
                return 
PLUGIN_HANDLED
            
}
        }
    
        
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(id)
{
    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)
    
    
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-24-2011 at 07:33.
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