PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <sqlx>
// Ur Mysql Information
new Host[] = "127.0.0.1"
new User[] = "amx_mac"
new Pass[] = "amx_mac"
new Db[] = "amx_mac"
new Handle:g_SqlTuple
new g_Error[512]
new iData[33]
public plugin_init() {
register_clcmd("say /steam", "Load_MySql")
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,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
if(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(SqlConnection, "CREATE TABLE IF NOT EXISTS tutorial (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, nameid varchar(32), steamid varchar(32), data 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)
// you free everything with SQL_FreeHandle
SQL_FreeHandle(SqlConnection)
}
public plugin_end() {
// free the tuple - note that this does not close the connection,
// since it wasn't connected in the first place
SQL_FreeHandle(g_SqlTuple)
}
public Load_MySql(id) {
new szName[32], szSteamId[32], szTemp[512]
get_user_authid(id,szSteamId,31)
get_user_name(id, szName,31)
new Data[1]
Data[0] = id
//we will now select from the table `tutorial` where the steamid match
format(szTemp,charsmax(szTemp), "SELECT * FROM `tutorial` WHERE (`tutorial`.`nameid` = '%s')", szName)
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 szName[32], szSteamId[32]
get_user_authid(id, szSteamId,31) // get user's steamid
get_user_name(id, szName,31) // get user's nameid
// if its still pending we can't do anything with it
if (equal(szName, "ID_PENDING")) return PLUGIN_HANDLED
new szTemp[512]
// now we will insturt the values into our table.
format(szTemp,charsmax(szTemp), "INSERT INTO `tutorial` ( `nameid` , `steamid` , `data`) VALUES ('%s' , '%s' , '0');", szName, szSteamId)
SQL_ThreadQuery(g_SqlTuple, "IgnoreHandle", szTemp)
} else {
// if there are results found
iData[id] = SQL_ReadResult(Query, 0)
}
return PLUGIN_HANDLED
}
public IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize) {
SQL_FreeHandle(Query)
return PLUGIN_HANDLED
}