Advertisement
Guest User

Untitled

a guest
May 7th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 3.51 KB | None | 0 0
  1. public MySql_Init()
  2. {
  3.     // we tell the API that this is the information we want to connect to,
  4.     // just not yet. basically it's like storing it in global variables
  5.     g_SqlTuple = SQL_MakeDbTuple(Host,Userz,Pass,Db)
  6.    
  7.     // ok, we're ready to connect
  8.     new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
  9.     if(SqlConnection == Empty_Handle)
  10.         // stop the plugin with an error message
  11.         set_fail_state(g_Error)
  12.        
  13.     new Handle:Queries
  14.     // we must now prepare some random queries
  15.     Queries = SQL_PrepareQuery(SqlConnection,"CREATE TABLE IF NOT EXISTS zmescape (name varchar(32),escapes INT(11))")
  16.  
  17.     if(!SQL_Execute(Queries))
  18.     {
  19.         // if there were any problems the plugin will set itself to bad load.
  20.         SQL_QueryError(Queries,g_Error,charsmax(g_Error))
  21.         set_fail_state(g_Error)
  22.        
  23.     }
  24.    
  25.     // Free the querie
  26.     SQL_FreeHandle(Queries)
  27.    
  28.     // you free everything with SQL_FreeHandle
  29.     SQL_FreeHandle(SqlConnection)  
  30. }  
  31.  
  32. public plugin_end()
  33. {
  34.     // free the tuple - note that this does not close the connection,
  35.     // since it wasn't connected in the first place
  36.     SQL_FreeHandle(g_SqlTuple)
  37. }
  38.  
  39. public Load_MySql(id)
  40. {
  41.     new szName[32], szTemp[512]
  42.     get_user_name(id, szName, charsmax(szName))
  43.    
  44.     new Data[1]
  45.     Data[0] = id
  46.    
  47.     //we will now select from the table `tutorial` where the steamid match
  48.     ////format(szTemp,charsmax(szTemp),"SELECT * FROM `zmescape` WHERE (`zmescape`.`name` = '%s')", szName)
  49.     //////format(szTemp,charsmax(szTemp),"SELECT * FROM `zmescape` WHERE (`zmescape`.`name` = '%s')", szName)
  50.     format(szTemp,charsmax(szTemp),"SELECT * FROM `zmescape` WHERE (`escapes`.`name` = '%s')", szName)
  51.     SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1)
  52. }
  53.  
  54. public register_client(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
  55. {
  56.     if(FailState == TQUERY_CONNECT_FAILED)
  57.     {
  58.         log_amx("Load - Could not connect to SQL database.  [%d] %s", Errcode, Error)
  59.     }
  60.     else if(FailState == TQUERY_QUERY_FAILED)
  61.     {
  62.         log_amx("Load Query failed. [%d] %s", Errcode, Error)
  63.     }
  64.  
  65.     new id
  66.     id = Data[0]
  67.    
  68.     if(SQL_NumResults(Query) < 1)
  69.     {
  70.         //.if there are no results found
  71.        
  72.         new szName[32]
  73.         get_user_name(id, szName, charsmax(szName)) // get user's steamid
  74.        
  75.         //  if its still pending we can't do anything with it
  76.         if (equal(szName,"ID_PENDING"))
  77.             return PLUGIN_HANDLED
  78.            
  79.         new szTemp[512]
  80.        
  81.         // now we will insturt the values into our table.
  82.         format(szTemp,charsmax(szTemp),"INSERT INTO `zmescape` ( `name` , `escapes`)VALUES ('%s','0');",szName)
  83.         SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
  84.     }
  85.     else
  86.     {
  87.         // if there are results found
  88.         escapes[id]         = SQL_ReadResult(Query, 1)
  89.     }
  90.    
  91.     return PLUGIN_HANDLED
  92. }  
  93.  
  94. public Save_MySql(id)
  95. {
  96.     new szName[32], szTemp[512]
  97.     get_user_name(id, szName, charsmax(szName))
  98.    
  99.     // Here we will update the user hes information in the database where the steamid matches.
  100.     format(szTemp,charsmax(szTemp),"UPDATE `zmescape` SET `escapes` = '%i' WHERE `zmescape`.`name` = '%s';",escapes[id], szName)
  101.     SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
  102. }  
  103.  
  104. public IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
  105. {
  106.     SQL_FreeHandle(Query)
  107.    
  108.     return PLUGIN_HANDLED
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement