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

03-22-2011
, 18:57
Re: query not returning results!?
|
#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(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] new userID = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "autoID")) 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, userID) 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 } new access[32] new len = formatex(access, 31, Data[0]) 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], 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") == -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") } } } SQL_NextRow(Query) } while(SQL_MoreResults(Query))
new name[17] get_user_name(Data[0], 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, access, ++len) 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[31] new name[17] new pass[11] get_user_name(Data[0], name, 16) get_user_info(Data[0], "_pw", pass, 10) formatex(access, 30, Data[2]) if(SQL_MoreResults(Query)) { formatex(cache, 511, "UPDATE admins SET access = '%s' WHERE auth = '%s' LIMIT 1;", access, name) SQL_ThreadQuery(g_SqlTuple, "handle_add_privs", cache) } else { 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, privID) { 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) formatex(cache, 511, "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(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-23-2011 at 17:49.
|
|