Re: Save Points On mysql
Try this :
PHP Code:
#include <amxmodx> #include <sqlx>
new PLUGIN[] = "Points" new AUTHOR[] = "Kia Armani" new VERSION[] = "1.0"
new Host[] = "" new User[] = "" new Pass[] = "" new Db[] = ""
new Points[33]; new bool:iStart[33];
new Handle:g_SqlConnection new Handle:g_SqlTuple new g_Error[512] new g_authed[33] new g_sql_ready = false new bool:g_loaded[33]
new maxplayers;
public plugin_init() { register_plugin(PLUGIN, VERSION, AUTHOR) maxplayers = get_maxplayers() set_task(0.1, "MySql_Init") }
public MySql_Init() { // we tell the API that this is the information we want to connect to, // just not yet. basically it's like storing it in global variables g_SqlTuple = SQL_MakeDbTuple(Host,User,Pass,Db) // ok, we're ready to connect new ErrorCode g_SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error)) if(g_SqlConnection == Empty_Handle) { // stop the plugin with an error message set_fail_state(g_Error) } new Handle:Queries // we must now prepare some random queries Queries = SQL_PrepareQuery(g_SqlConnection,"CREATE TABLE IF NOT EXISTS pts (steamid varchar(32), name varchar(64), money INT(11))") if(!SQL_Execute(Queries)) { // if there were any problems SQL_QueryError(Queries,g_Error,charsmax(g_Error)) set_fail_state(g_Error) } // close the handle SQL_FreeHandle(Queries) g_sql_ready = true for(new i=1; i<=maxplayers; i++) { if(g_authed[i]) { Load_MySql(i) } } }
public Load_MySql(id) { if(g_sql_ready) { if(g_SqlTuple == Empty_Handle) { set_fail_state(g_Error) } new szSteamId[32], szTemp[512] get_user_authid(id, szSteamId, charsmax(szSteamId)) new Data[1] Data[0] = id //we will now select from the table `furienmoney` where the steamid match format(szTemp,charsmax(szTemp),"SELECT * FROM `pts` WHERE (`pts`.`steamid` = '%s')", szSteamId) SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1) } }
public register_client(FailState,Handle:Query,Error[],Errcode,Data[],DataSize) { if(FailState == TQUERY_CONNECT_FAILED) { log_amx("Load - Could not connect to SQL database. [%d] %s", Errcode, Error) } else if(FailState == TQUERY_QUERY_FAILED) { log_amx("Load Query failed. [%d] %s", Errcode, Error) } new id id = Data[0] if(SQL_NumResults(Query) < 1) { //.if there are no results found new szSteamId[32], szName[32], szQuotedName[64] get_user_authid(id, szSteamId, charsmax(szSteamId)) // get user's steamid get_user_name(id, szName, 31) // get user's name SQL_QuoteString(g_SqlConnection, szQuotedName, 63, szName) // if its still pending we can't do anything with it if (equal(szSteamId,"ID_PENDING")) { return PLUGIN_HANDLED } new szTemp[512] // now we will insert the values into our table. format(szTemp,charsmax(szTemp),"INSERT INTO `coins` ( `steamid` , `name` , `pts`) VALUES ('%s','%s','0');",szSteamId, szQuotedName) SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp) } else { // if there are results found Points[id] = SQL_ReadResult(Query, 2) } g_loaded[id] = true return PLUGIN_HANDLED }
public Save_MySql(id) { if(g_loaded[id]) { new szSteamId[32], szTemp[512], szName[32], szQuotedName[64] get_user_authid(id, szSteamId, charsmax(szSteamId)) get_user_name(id, szName, 31) SQL_QuoteString(g_SqlConnection, szQuotedName, 63, szName) // Here we will update the user hes information in the database where the steamid matches. format(szTemp,charsmax(szTemp),"UPDATE `coins` SET `name` = '%s', `pts` = '%i' WHERE `coins`.`steamid` = '%s';",szQuotedName,Points[id],szSteamId) SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp) } }
public IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize) { SQL_FreeHandle(Query) return PLUGIN_HANDLED }
public plugin_end() { if(g_SqlConnection != Empty_Handle) { SQL_FreeHandle(g_SqlConnection) //free connection handle here } }
public client_disconnect(id) { Save_MySql(id) Points[id] = 0 iStart[id] = false }
public client_authorized(id) { g_authed[id] = true Load_MySql(id) }
|