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

03-23-2011
, 09:05
Re: query not returning results!?
|
#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(PLUGIN, VERSION, AUTHOR); sql_init() }
public sql_init() { g_SqlTuple = SQL_MakeDbTuple(HOST, USER, PASS, DB) new err[512], err_code g_SqlCon = SQL_Connect(g_SqlTuple, err_code, err, 511) if(g_SqlCon == Empty_Handle) { log_amx("SQL Error: %s (%d)", err, err_code) } }
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], quotedName[17] get_user_name(id, name, 16) SQL_QuoteString(g_SqlCon, quotedName, 16, name) new cache[512] formatex(cache, 511, "SELECT * FROM %s WHERE UserName='%s';", g_sqlTable_users, quotedName) 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[34], pass_check[25], pass_buff[34] 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, 33) get_user_info(Data[0], "_pw", pass_check, 24) md5(pass_check, pass_buff) if(compare_arrays(pass, pass_buff, 33)) { 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, "%d", 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") } } } SQL_NextRow(Query) } while(SQL_MoreResults(Query))
new name[17], quotedName[17] get_user_name(Data[0], name, 16) SQL_QuoteString(g_SqlCon, quotedName, 16, name) new cache[512] formatex(cache, 511, "SELECT * FROM admins WHERE auth='%s' LIMIT 1;", quotedName) 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], quotedName[17] new pass[25], quotedPass[25] get_user_name(str_to_num(Data[0]), name, 16) get_user_info(str_to_num(Data[0]), "_pw", pass, 10) SQL_QuoteString(g_SqlCon, quotedName, 16, name) SQL_QuoteString(g_SqlCon, quotedPass, 24, pass) formatex(access, 30, Data[1]) if(SQL_MoreResults(Query)) { formatex(cache, 511, "UPDATE admins SET access = '%s' WHERE auth = '%s' LIMIT 1;", access, quotedName) SQL_ThreadQuery(g_SqlTuple, "handle_add_privs", cache) } else { formatex(cache, 511, "INSERT INTO admins VALUES('%s', '%s', '%s', 'k');", quotedName, quotedPass, access) SQL_ThreadQuery(g_SqlTuple, "handle_add_privs", cache) } return PLUGIN_HANDLED }
public remove_privs(id, privID) { new name[17], quotedName[17] get_user_name(id, name, 16) SQL_QuoteString(g_SqlCon, quotedName, 16, name) new cache[512] formatex(cache, 511, "DELETE FROM admins WHERE auth='%s' LIMIT 1;", quotedName) SQL_ThreadQuery(g_SqlTuple, "handle_remove_privs", cache) formatex(cache, 511, "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(FailState, Handle:Query, Error[], Errcode, Data[], DataSize) { if(FailState) { log_amx("SQL Error: %s (%d)", Error, Errcode) return PLUGIN_HANDLED } server_cmd("amx_reloadadmins") return PLUGIN_HANDLED }
public handle_remove_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) if(g_SqlCon != Empty_Handle) SQL_FreeHandle(g_SqlCon) }
__________________
Last edited by SeriousSam; 03-25-2011 at 09:07.
|
|