|
Senior Member
Join Date: Aug 2009
Location: Bulgaria
|

03-22-2011
, 15:14
[SOLVED] query not returning results!?
|
#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(PLUGIN, VERSION, AUTHOR); sql_init() }
public sql_init() { g_SqlTuple = SQL_MakeDbTuple(HOST, USER, PASS, DB) }
bool:compare_arrays(array1[], array2[], size) { new i for (i=0; i<size; i++) { 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(id, name, 16)
new cache[512] formatex(cache, 511, "SELECT * FROM %s WHERE UserName='%s';", g_sqlTable_users, name) SQL_ThreadQuery(g_SqlTuple, "handle_user", cache, Data, 1) return PLUGIN_HANDLED }
public handle_user(FailState, Handle:Query,Error[], Errcode, Data[], DataSize) { if(FailState) { log_amx("SQL Error: %s (%d)", Error, Errcode) return PLUGIN_HANDLED } if(SQL_MoreResults(Query)) { new name[17], pass[11], pass_check[11] SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "UserName"), name, 16) SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "Password"), pass, 10) get_user_info(Data[0], "_pw", pass_check, 10) if(compare_arrays(pass, pass_check, 10)) { new cache[512] formatex(cache, 511, "SELECT * FROM %s WHERE userID='%d';", g_sqlTable_privs, Data[0]) SQL_ThreadQuery(g_SqlTuple, "handle_priv", cache, Data, 1) } } else { log_amx("retrieved no results ....") }
return PLUGIN_HANDLED }
public handle_priv(FailState, Handle:Query, Error[], Errcode, Data[], DataSize) { if(FailState) { log_amx("SQL Error: %s (%d)", Error, Errcode) return PLUGIN_HANDLED }
if(!SQL_MoreResults(Query)) { log_amx("retrieved no results ....") return PLUGIN_HANDLED } do { new privID = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "privID")) new autodate = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "autodate")) new period = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "period")) new final_moment = autodate + 86400*period if(get_systime() > final_moment) remove_privs(Data[0]) else check_privs(Data[0], privID) SQL_NextRow(Query) } while(SQL_MoreResults(Query))
return PLUGIN_HANDLED }
public check_privs(id, privID) { new Data[2] Data[0] = id Data[1] = privID
new name[17] get_user_name(id, name, 16) new cache[512] formatex(cache, 511, "SELECT * FROM admins WHERE auth='%s' LIMIT 1;", name) SQL_ThreadQuery(g_SqlTuple, "handle_check_privs", cache, Data, 2) return PLUGIN_HANDLED }
public handle_check_privs(FailState, Handle:Query, Error[], Errcode, Data[], DataSize) { if(FailState) { log_amx("SQL Error: %s (%d)", Error, Errcode) return PLUGIN_HANDLED } new cache[512] new access[32] new name[17] new pass[11] new len get_user_name(Data[0], name, 16) get_user_info(Data[0], "_pw", pass, 10) if(SQL_MoreResults(Query)) { SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "access"), access, 31) len = format(access, 31, "%s", access) switch(Data[1]) { case 1: { if(containi(access, "a") == -1) len += formatex(access[len], 31-len, "a") } case 4: { if(containi(access, "b") == -1) len += formatex(access[len], 31-len, "b") } case 7: { if(containi(access, "f") == -1) len += formatex(access[len], 31-len, "f") } case 10: { if(containi(access, "j") == -1) len += formatex(access[len], 31-len, "j") } case 13: { if(containi(access, "a") == -1) len += formatex(access[len], 31-len, "a") if(containi(access, "b") == -1) len += formatex(access[len], 31-len, "b") if(containi(access, "f") == -1) len += formatex(access[len], 31-len, "f") if(containi(access, "j") == -1) len += formatex(access[len], 31-len, "j") } default: { return PLUGIN_HANDLED } } formatex(cache, 511, "UPDATE admins SET access = '%s' WHERE auth = '%s' LIMIT 1;", access, name) 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(cache, 511, "INSERT INTO admins VALUES('%s', '%s', '%s', 'k');", name, pass, access) SQL_ThreadQuery(g_SqlTuple, "handle_add_privs", cache) }
return PLUGIN_HANDLED }
public remove_privs(id) { new name[17] get_user_name(id, name, 16) new cache[512] formatex(cache, 511, "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(FailState, Handle:Query, Error[], Errcode, Data[], DataSize) { if(FailState) { log_amx("SQL Error: %s (%d)", Error, Errcode) 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.
|
|