Fury Posted April 19, 2012 Share Posted April 19, 2012 -- Database connection info sqlHostname = "***" sqlUsername = "***" sqlPassword = "***" sqlDefaultDatabase = "***" sqlDefaultTable = "userdata" -- Connect to the database function mySQLConnect() sqlConnection = mysql_connect(sqlHostname,sqlUsername,sqlPassword,sqlDefaultDatabase) end addEventHandler("onResourceStart",getResourceRootElement(getThisResource()),mySQLConnect) -- Make a new entry to the mySQL database function checkSqlData(source) if (sqlConnection == nil) then mySQLConnect() end local player = source local serial = getPlayerSerial(player) local entryCheck = sqlConnection:query("SELECT serial FROM "..sqlDefaultTable.." WHERE serial='"..serial.."'") if (entryCheck) then local result = mysql_result(entryCheck,1,1) if (result == "") or (result == nil) then outputDebugString("New entry added to the mySQL database!") sqlConnection:query("INSERT INTO "..sqlDefaultTable.." (serial) VALUES ('"..serial.."')") sqlConnection:query("UPDATE "..sqlDefaultTable.." SET playerName='"..string.gsub(getPlayerName(player),"#%x%x%x%x%x%x", "").."' WHERE serial='"..serial.."'") end else outputDebugString("New entry added to the mySQL database!") sqlConnection:query("INSERT INTO "..sqlDefaultTable.." (serial) VALUES ('"..serial.."')") sqlConnection:query("UPDATE "..sqlDefaultTable.." SET playerName='"..string.gsub(getPlayerName(player),"#%x%x%x%x%x%x", "").."' WHERE serial='"..serial.."'") end end addEventHandler("onPlayerJoin",getRootElement(),checkSqlData) addEventHandler("onResourceStart",getResourceRootElement(getThisResource()), function() for i,player in ipairs (getElementsByType("player")) do checkSqlData(player) end end) -- Load data from the database function loadPlayerData (player,datatype) if (sqlConnection == nil) then restartResource(getThisResource()) end if (player) and (datatype) then local serial = getPlayerSerial(player) local findQuery = sqlConnection:query("SELECT "..datatype.." FROM "..sqlDefaultTable.." WHERE serial='"..serial.."'") if (findQuery) then local result = mysql_result(findQuery,1,1) if not (result == nil) then if not (result == "") then mysql_free_result(findQuery) return result else mysql_free_result(findQuery) return 0 end else return 0 end else outputDebugString("Failed to get "..datatype.." for player "..getPlayerName(player).." @ findQuery") outputDebugString("mysql_query failed: (" .. mysql_errno(sqlConnection) .. ") " .. mysql_error(sqlConnection)) end end end -- Save data to the database function savePlayerData (player,datatype,newvalue) if (sqlConnection == nil) then restartResource(getThisResource()) end if (player) and (datatype) and (newvalue) then local serial = getPlayerSerial(player) local saveData = sqlConnection:query("UPDATE "..sqlDefaultTable.." SET "..datatype.."='"..newvalue.."' WHERE serial='"..serial.."'") if (saveData == nil) then outputDebugString("Error executing the save query: (" .. mysql_errno(sqlConnection) .. ") " .. mysql_error(sqlConnection)) end end end what is wrong with this? code by: NeXtReMe Link to comment
myonlake Posted April 19, 2012 Share Posted April 19, 2012 Are you sure your connection details are correct? And are you sure you have the latest MySQL module? Link to comment
Fury Posted April 19, 2012 Author Share Posted April 19, 2012 Are you sure your connection details are correct? And are you sure you have the latest MySQL module? yes and i dont know. how can i install it? Link to comment
Absence2 Posted April 19, 2012 Share Posted April 19, 2012 https://wiki.multitheftauto.com/wiki/Modules/MTA-MySQL Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now