Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. -- Settings
  2. DBHandler=nil
  3. DBName="country"
  4. DBUser="root"
  5. DBPass=""
  6. DBHost="localhost"
  7.  
  8. -- Functions
  9. function dbSet(...)
  10. if not {...} then return end
  11. local stringe=dbPrepareString(DBHandler,...)
  12. local query=dbExec(DBHandler, stringe)
  13. return query
  14. end
  15.  
  16. function dbGet(...)
  17. if not {...} then return end
  18. local stringe=dbPrepareString(DBHandler,...)
  19. local query=dbQuery(DBHandler, stringe)
  20. local result=dbPoll(query, -1)
  21. return result
  22. end
  23.  
  24.  
  25. addEventHandler("onResourceStart", resourceRoot, function()
  26. DBHandler=dbConnect("mysql", "dbname="..DBName..";host="..DBHost.."", DBUser, DBPass, "share=1;autoreconnect=1")
  27. if DBHandler then
  28. outputDebugString("* Connect to server MYSQL...")
  29. dbSet("SET NAMES utf8")
  30. else
  31. outputDebugString("* No Connecting to server MYSQL..")
  32. end
  33. end)
  34.  
  35.  
  36. local SQL_LOGIN="root"
  37. local SQL_PASSWD=""
  38. local SQL_DB="country"
  39. local SQL_HOST="localhost"
  40. local SQL_PORT=3306
  41.  
  42. local root = getRootElement()
  43.  
  44. local SQL
  45.  
  46. local function connect()
  47. SQL = mysql_connect(SQL_HOST, SQL_LOGIN, SQL_PASSWD, SQL_DB, SQL_PORT)
  48. if (not SQL) then
  49. outputServerLog("BRAK POLACZENIA Z BAZA DANYCH!")
  50. else
  51. --mysql_query(SQL,"SET NAMES utf8")
  52. --outputServerLog("Modul mysql polaczony!")
  53. end
  54.  
  55. end
  56.  
  57.  
  58. local function keepAlive()
  59. if (not mysql_ping(SQL)) then
  60. outputServerLog("Zerwane polaczenie z baza danych, nawiazywanie...")
  61. connect()
  62. end
  63. end
  64. addEventHandler("onResourceStart",getResourceRootElement(),function()
  65. connect()
  66. setTimer(keepAlive, 30000, 0)
  67. end)
  68.  
  69. function esc(value)
  70. return mysql_escape_string(SQL,value)
  71. end
  72.  
  73. function pobierzTabeleWynikow(query)
  74. local result=mysql_query(SQL,query)
  75. if (not result) then
  76. outputDebugString("mysql_query failed: (" .. mysql_errno(SQL) .. ") " .. mysql_error(SQL))
  77. outputServerLog("mysql_query failed: (" .. mysql_errno(SQL) .. ") " .. mysql_error(SQL))
  78. return nil
  79. end
  80. local tabela={}
  81. for result,row in mysql_rows_assoc(result) do
  82. table.insert(tabela,row)
  83. end
  84. mysql_free_result(result)
  85. return tabela
  86. end
  87.  
  88. function pobierzWyniki(query)
  89. local result=mysql_query(SQL,query)
  90. if (not result) then return nil end
  91. row = mysql_fetch_assoc(result)
  92. mysql_free_result(result)
  93. return row
  94. end
  95.  
  96.  
  97. function zapytanie(query)
  98. local result=mysql_query(SQL,query)
  99. if (result) then mysql_free_result(result) return true end
  100. return
  101. end
  102.  
  103. function insertID()
  104. return mysql_insert_id(SQL)
  105. end
  106.  
  107. function affectedRows()
  108. return mysql_affected_rows(SQL)
  109. end
  110.  
  111.  
  112. function fetchRows(query)
  113. local result=mysql_query(SQL,query)
  114. if (not result) then return nil end
  115. local tabela={}
  116.  
  117. while true do
  118. local row = mysql_fetch_row(result)
  119. if (not row) then break end
  120. table.insert(tabela,row)
  121. end
  122. mysql_free_result(result)
  123. return tabela
  124. end
  125.  
  126.  
  127. function getSQLLink()
  128. return SQL
  129. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement