| turshija |
06-13-2010 11:06 |
Re: Quick SQL help
Okay, I've managed to make exactly what I need (simple ban that uses SQL) , but I'm getting an error messages I don't know how to fix :(
Code:
L 06/13/2010 - 15:19:45: [MySQL] Invalid info tuple handle: 0
L 06/13/2010 - 15:19:45: [AMXX] Displaying debug trace (plugin "amx_cheater.amxx")
L 06/13/2010 - 15:19:45: [AMXX] Run time error 10: native error (native "SQL_ThreadQuery")
L 06/13/2010 - 15:19:45: [AMXX] [0] amx_cheater.sma::client_connect (line 70)
Error logs spammed with this, and server crashes too much ... :S
line 70 is this:
PHP Code:
SQL_ThreadQuery( g_sql_tuple, "QueryCheckAuth", cache, data, 1);
And this is entire code:
PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <sqlx>
new pc_sql_host
new pc_sql_user
new pc_sql_pass
new pc_sql_db
new pc_site
new pc_unique
new Handle:g_sql_tuple
new const link[] = "http://www.site.com/ban/addban.php";
public plugin_init() {
register_plugin("SQL Ban", "1.0", "Turshija");
register_concmd("amx_banz", "banuj", ADMIN_BAN, "Name Time ^"Reason^"!");
pc_sql_host = register_cvar("db_host", "xxx.xxx.xxx.xxx");
pc_sql_user = register_cvar("db_user", "db user");
pc_sql_pass = register_cvar("db_pass", "db pass", FCVAR_PROTECTED);
pc_sql_db = register_cvar("db_name", "db name");
pc_site = register_cvar("sql_ban_site",""); // forum link
pc_unique = register_cvar("sql_ban_unique",""); // unique server ID
set_task(0.1, "grab_cvars")
}
public grab_cvars(){
new sql_host[32], sql_user[32], sql_pass[32], sql_db[32]
get_pcvar_string(pc_sql_host, sql_host, 31)
get_pcvar_string(pc_sql_user, sql_user, 31)
get_pcvar_string(pc_sql_pass, sql_pass, 31)
get_pcvar_string(pc_sql_db, sql_db, 31)
g_sql_tuple = SQL_MakeDbTuple(sql_host, sql_user, sql_pass, sql_db)
}
public client_connect(id) {
new name[32],auth[32];
new cache[500];
new unique[32];
get_user_name(id, name, 31);
get_user_authid(id, auth, 31);
get_pcvar_string(pc_unique,unique,31);
new tajm = get_systime();
formatex(cache,500, "SELECT * FROM bans WHERE banned='1' AND steamid='%s' AND uniqueid='%s' AND expire>'%i';",auth,unique,tajm);
// here..
new data[1];
data[0] = id;
// and here.. the last parameters
SQL_ThreadQuery( g_sql_tuple, "QueryCheckAuth", cache, data, 1);
return PLUGIN_CONTINUE;
}
public QueryCheckAuth( iFailState, Handle:hQuery, szError[ ], iError, iData[ ], iDataSize, Float:fQueueTime ) {
if( iFailState == TQUERY_CONNECT_FAILED || iFailState == TQUERY_QUERY_FAILED ) {
log_amx( "SQL Error!" );
} else if( SQL_NumResults( hQuery ) ) {
//taking the ID previously sent through 'data'.
new id = iData[0];
new banid[32],admin[32],reason[500],datum[32],istek[32],temp;
new website[64];
get_pcvar_string(pc_site,website,63);
SQL_MoreResults( hQuery );
SQL_ReadResult(hQuery, 0, banid,31);
SQL_ReadResult(hQuery, 3,admin, 31);
SQL_ReadResult(hQuery, 5,reason, 499);
SQL_ReadResult(hQuery, 6,datum, 31);
SQL_ReadResult(hQuery, 10,istek, 31);
temp = ((str_to_num(istek)-get_systime())/60)+1;
// prints ban stuff in console
client_cmd(id,"clear");
client_cmd(id,"echo;echo;echo;echo;echo;echo;echo;");
client_cmd(id,"echo =====================================");
client_cmd(id,"echo Banned by: ^"%s^"",admin);
// also echoes ban time, expiration time, etc, etc
client_cmd(id,"echo =====================================");
client_cmd(id,"echo;echo;echo;echo;echo;");
set_task(1.0,"kikuj",id,"",0,"a",5)
}
}
public kikuj(id) {
server_cmd("kick #%d ^"You are banned on this server, check console for more info!^"",get_user_userid(id));
}
public banuj(id, level, cid) {
if(!cmd_access(id, level, cid, 2))
return 1;
new target[32], reason[51];
new TaskData[4];
new temp[8],duzina;
read_argv(1, target, 31);
new iTarget = cmd_target(id, target, 8);
if (!iTarget)
return PLUGIN_HANDLED;
new argc = read_argc();
if (argc == 1) {
temp="0";
reason="No reason";
} else if (argc == 2) {
read_argv(2, temp,7);
reason="No reason";
} else {
read_argv(2, temp,7); // ban length
read_argv(3, reason, 50);
}
duzina = str_to_num(temp); // ban length
TaskData[0]=id;
TaskData[1]=iTarget;
TaskData[2]=duzina;
//client_print(id,print_chat,"LOL HAI XD");
motd(TaskData,reason);
return PLUGIN_HANDLED;
}
public motd(TaskData[],reason[])
{
new id = TaskData[0];
new target = TaskData[1];
new duzina = TaskData[2];
new temp=duzina;
if (temp==0) temp=1000000;
new BanTime = ((temp * 60) + get_systime());
new cache[1000]="",cache2[1000]="";
new adminname[32]; new cheatername[32]; new cheaterauth[32]; new cheaterip[32]; new datum[32]; new hostname[64];
new website[64],unique[32];
get_pcvar_string(pc_site,website,63);
get_pcvar_string(pc_unique,unique,31);
get_user_name(id, adminname, 31);
get_user_name(target, cheatername, 31);
get_user_authid(target, cheaterauth, 31);
get_user_ip(target, cheaterip, 31);
strcat(cache,link,100);
formatex(cache2, 1000, "?name=%s&steamid=%s&adminname=%s&ip=%s&reason=%s&unique=%s&expire=%i",cheatername,cheaterauth, adminname, cheaterip, reason, unique, BanTime)
strcat(cache,cache2,1000);
show_motd(id,cache)
get_cvar_string("hostname",hostname,63);
get_time("%H:%M - %m/%d/%Y", datum,31);
// prints info in chat for screenshot
client_print(target, print_chat,"* Banovao te %s na serveru: %s",adminname,hostname);
client_print(target, print_chat, "* Trenutni nick: ^"%s^" SteamID: %s ",cheatername,cheaterauth);
client_print(target, print_chat, "* Datum : %s",datum);
client_print(target, print_chat, "* Poseti %s za unban.",website);
console_print(target, "* Banovao te %s na serveru: %s",adminname,hostname);
console_print(target, "* Trenutni nick: ^"%s^" SteamID: %s ",cheatername,cheaterauth);
console_print(target, "* Datum : %s",datum);
console_print(target, "* Poseti %s za unban.",website);
client_cmd(target,"wait;wait;wait;wait;wait;wait;wait;wait;wait;wait;wait;wait;wait;wait;wait;");
client_cmd(target,"snapshot;wait;wait;wait;screenshot;wait;snapshot;wait;wait;wait;retry;");
return PLUGIN_HANDLED
}
this ban works basically by opening a MOTD window on admin that links to a PHP file that inserts ban info in SQL, and then execing retry on player so he gets ban info in console printed, and it works excellent ...
Main reason I'm doing that is because I made SQL user that has only SELECT privilege so people can't sniff SQL pass and fuk up DB :)
When I made it without Threaded query using this:
PHP Code:
Query = SQL_PrepareQuery(g_SqlConnection, "SELECT * FROM bans WHERE banned='1' AND steamid='%s' AND uniqueid='%s';", auth,unique)
SQL_Execute(Query);
it worked fine, but I thought threaded query would be better ...
Also when testing this plugin on empty server, everything is fine, but after putting it on public server that has 30 players online, it crashes server sometimes on map change ...
Do I need SQL disconnect somewhere or something?
Any suggestions ?
Edit: I've copied everything from grab_cvars function into public client_connect, and so far it works great, but still if you think anything should be improved, feel free to tell me :)
|