Raised This Month: $ Target: $400
 0% 

[SOLVED] query not returning results!?


Post New Thread Reply   
 
Thread Tools Display Modes
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
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 03-22-2011 , 16:02   Re: query not returning results!?
Reply With Quote #2

1. You are using player names in queries without backquoting them (with SQL_QuoteString function). It's fine while testing, but you must fix it or it may cause serious problems if you use it on public server.
2. In handle_user function there is a SELECT query using player id (value 1-32) as a key, which makes no sense to me.
3. Instead of sending 2 queries - first to check password and then getting privs you can do it with just one if you use different table - SELECT * FROM privs WHERE username='%s' AND password='%s';.
4. Instead of messing with strings, you can store all privs as integer. There is tut about operations on bits: http://forums.alliedmods.net/showthread.php?t=139916
5. You didn't post definitions of tables. Are you sure that this query: "INSERT INTO admins VALUES('%s', '%s', '%s', 'k');" inserts everything into proper columns?
__________________
Impossible is Nothing
Sylwester is offline
SeriousSam
Senior Member
Join Date: Aug 2009
Location: Bulgaria
Old 03-22-2011 , 16:53   Re: query not returning results!?
Reply With Quote #3

1. Thanks for the tip. Will do. Though I don't know how that works

2. This was ok when I first wrote it, but after all the modifications I guess I changed it by mistake. Fixed it now:
new userID = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "autoID"))
formatex(cache, 511, "SELECT * FROM %s WHERE userID='%d';", g_sqlTable_privs, userID)

3. I can't do that, the password and username is in a different table than the one with the privs. Though maybe I might be able to do it with a more complex query by left joining the two tables, but I'm too lazy to think it up when this way works as well

4. Might do that.

5. Yes it should insert it properly. The admins table is created with the admin_sql plugin that comes with amxx. It has auth, password, access and flags fields (eg. user's name, user's pass, abcdefgh..., k)
The users table contains users from the website, I believe their fields are pretty self explainatory.
The users_privileges table contains the privileges that the users have purchased, with an autoID (primary key auto increment), userID which is the same as autoID from users table, priv id that tells which priv it is, autodate which is timestamp from the purchase moment, and period which is how many days is it purchased for. Hence in handle_priv func:
new final_moment = autodate + 86400*period //the timestamp(seconds since epoch launch) + days*seconds a day - gives the moment when the priv expires
if(get_systime() > final_moment) remove the priv from user_privileges
else add the priv in admins table, where it will be read from the game

EDIT: after fixing 2. the problem still exists, just as I thought.
after trying to add a and b flags, instead of getting "miromont, topsecret, ab, k" in the admins table, I get
"miromont, topsecret, b, k
miromont, topsecret, a, k"
So either the select query in the check_privs func doesn't return results, or the check for results in handle_check_privs thinks there are no results to update and goes to insert a new line. Or maybe there is even greater problem, dunno. Either way, I can't find the reason for this problem...
__________________

Last edited by SeriousSam; 03-22-2011 at 17:29.
SeriousSam is offline
Send a message via Skype™ to SeriousSam
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 03-22-2011 , 17:35   Re: query not returning results!?
Reply With Quote #4

The problem may be caused by do while loop. If there is no entry in admins table with auth you are using and then you execute check_privs(Data[0], privID) twice then the result will be obviously 2 inserts.
__________________
Impossible is Nothing
Sylwester is offline
SeriousSam
Senior Member
Join Date: Aug 2009
Location: Bulgaria
Old 03-22-2011 , 17:51   Re: query not returning results!?
Reply With Quote #5

That sounds highly probable, but is it me that doesn't understand why, shouldn't the do-while loop execute check_privs, see there is no entry, insert an entry, then the do-while loop moves to the next priv, executes check_privs for it, sees that there is already an entry, and add the second priv with update? At least thats how I thought it would work, but obviously not... Do you have any suggestion how can I make it work the way I want then?
__________________
SeriousSam is offline
Send a message via Skype™ to SeriousSam
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 03-22-2011 , 18:12   Re: query not returning results!?
Reply With Quote #6

Do while loop executes check_privs (sends SELECT query), then it moves to the next check_privs (sends SELECT query) and so on. The check to see if there is an entry happens at another time when results are retrieved from database, so the query that inserts data if there are no entries is sent after all SELECT queries from do while loop were sent.

To fix it you can simply concatenate all data that you want to send using do while loop and send it using 1 query.
__________________
Impossible is Nothing
Sylwester is offline
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
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 03-23-2011 , 08:20   Re: query not returning results!?
Reply With Quote #8

The problem is probably somewhere in handle_priv or in handle_check_privs. If you can't find it while looking at the code, then debug it.
__________________
Impossible is Nothing

Last edited by Sylwester; 03-23-2011 at 08:23.
Sylwester is offline
SeriousSam
Senior Member
Join Date: Aug 2009
Location: Bulgaria
Old 03-23-2011 , 09:05   Re: query not returning results!?
Reply With Quote #9

I think I found the problem. "new len = formatex(access, 31, Data[0])" to "new len = formatex(access, 31, "%d", Data[0])", because it doesn't add the '\0' char the other way It should work fine now, I'll test it in a sec.

EDIT: done testing, seems to work fine now. I changed the map 3-4 times with no problems. I'll do some more testing just to make sure it works. Under the post is the final code - I had to change a few more things to accommodate to the change, did some improvements too

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

// Plugin information
#define PLUGIN "Miro's Admin System"
#define VERSION "1.4"
#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 Handle:g_SqlCon
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)
    
    new 
err[512], err_code
    g_SqlCon 
SQL_Connect(g_SqlTupleerr_codeerr511)
    
    if(
g_SqlCon == Empty_Handle)
    {
        
log_amx("SQL Error: %s (%d)"errerr_code)
    }
}

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], quotedName[17]
    
get_user_name(idname16)
    
SQL_QuoteString(g_SqlConquotedName16name)
    
    new 
cache[512]
    
formatex(cache511"SELECT * FROM %s WHERE UserName='%s';"g_sqlTable_usersquotedName)
    
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[34], pass_check[25], pass_buff[34]
        new 
userID SQL_ReadResult(QuerySQL_FieldNameToNum(Query"autoID"))
        
SQL_ReadResult(QuerySQL_FieldNameToNum(Query"UserName"), name16)
        
SQL_ReadResult(QuerySQL_FieldNameToNum(Query"Password"), pass33)
        
        
get_user_info(Data[0], "_pw"pass_check24)
        
md5(pass_checkpass_buff)
        
        if(
compare_arrays(passpass_buff33))
        {
            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(access31"%d"Data[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")
                }
            }
        }
        
        
SQL_NextRow(Query)
    }
    while(
SQL_MoreResults(Query))

    new 
name[17], quotedName[17]
    
get_user_name(Data[0], name16)
    
SQL_QuoteString(g_SqlConquotedName16name)
    
    new 
cache[512]
    
formatex(cache511"SELECT * FROM admins WHERE auth='%s' LIMIT 1;"quotedName)
    
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], quotedName[17]
    new 
pass[25], quotedPass[25]
    
    
get_user_name(str_to_num(Data[0]), name16)
    
get_user_info(str_to_num(Data[0]), "_pw"pass10)
    
SQL_QuoteString(g_SqlConquotedName16name)
    
SQL_QuoteString(g_SqlConquotedPass24pass)
    
    
formatex(access30Data[1])
    
    if(
SQL_MoreResults(Query))
    {
        
formatex(cache511"UPDATE admins SET access = '%s' WHERE auth = '%s' LIMIT 1;"accessquotedName)
        
SQL_ThreadQuery(g_SqlTuple"handle_add_privs"cache)
    }
    else
    {    
        
formatex(cache511"INSERT INTO admins VALUES('%s', '%s', '%s', 'k');"quotedNamequotedPassaccess)
        
SQL_ThreadQuery(g_SqlTuple"handle_add_privs"cache)
    }
    
    return 
PLUGIN_HANDLED
}

public 
remove_privs(idprivID)
{
    new 
name[17], quotedName[17]
    
get_user_name(idname16)
    
SQL_QuoteString(g_SqlConquotedName16name)
    
    new 
cache[512]
    
    
formatex(cache511"DELETE FROM admins WHERE auth='%s' LIMIT 1;"quotedName)
    
SQL_ThreadQuery(g_SqlTuple"handle_remove_privs"cache)
    
    
formatex(cache511"DELETE FROM user_privileges WHERE privID='%d' LIMIT 1;"privID)
    
SQL_ThreadQuery(g_SqlTuple"handle_remove_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
    
}
    
    
server_cmd("amx_reloadadmins")
    return 
PLUGIN_HANDLED
}

public 
handle_remove_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)
    
    if(
g_SqlCon != Empty_HandleSQL_FreeHandle(g_SqlCon)

__________________

Last edited by SeriousSam; 03-25-2011 at 09:07.
SeriousSam is offline
Send a message via Skype™ to SeriousSam
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 14:29.


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