Hey!
I'm starting to make a player logging script to new players, but my MySQLoo does not want to connect my test MySQL server. Here is my code
DATABASE_HOST = "localhost"
DATABASE_PORT = 3306
DATABASE_NAME = "server"
DATABASE_USERNAME = "root"
DATABASE_PASSWORD = ""
local MySQLOO = require("mysqloo")
DB_CONN = mysqloo.connect("localhost","root","", "server",3306)
function ConnectToDB()
print("[SQL] "..os.date( "%I:%M:%S %p").." - Connecting to Database!")
DB_CONN.onConnected = function()
print("[SQL] "..os.date( "%I:%M:%S %p").." - Database Connection Successful!")
end
DB_CONN.onConnectionFailed = function(db, msg)
print("[SQL] "..os.date( "%I:%M:%S %p").." - Connection to Database failed!")
print(msg)
end
end
ConnectToDB()
local function CheckDB()
if (DB_CONN:status() != mysqloo.DATABASE_CONNECTED) then
print("[SQL] "..os.date("%I:%M:%S %p").." - Database Connection Restarted!")
ConnectToDB()
end
end
timer.Create("CDB",5,0,CheckDB)
function SearchDatabase(ply)
if ply == nil then return end
local query = DB_CONN:query("SELECT id FROM test WHERE steamid ='"..ply:SteamID().."';")
if not query then return end
query.onData = function(q,d)
if (#query:getData() >=1) then
ply:ChatPrint("Du er spiller nummer: #"..tostring(d['id']))
end
end
query.onError = function(db, err)
print("[SQL | SearchDatabase] "..os.date( "%I:%M:%S %p").." - Error: ",err)
end
query:wait()
query:start()
end
function AddPlayer(id)
local query = DB_CONN:query("INSERT INTO test VALUES ('', '"..id.."');")
query.onSuccess = function()
print("[SQL] "..os.date( "%I:%M:%S %p").." - En ny spiller, ved ID'et: "..id.." var logged!")
end
query.onError = function(db, err)
print("[SQL | AddPlayer] "..os.date( "%I:%M:%S %p").." - Error: ", err)
end
query:start()
end
hook.Add("PlayerInittialSpawn","AddPlayerToDB",function(ply)
AddPlayer(ply:SteamID())
end)
hook.Add("PlayerSay","CheckPlayerCMD",function(ply, txt, pub)
if string.sub( string.lower(txt), 1,4) == "!sql" then
SearchDatabase(ply)
return ""
end
end)
Of course it's not connecting to the database because you aren't telling the server to connect to the database. Try doing DB_CONN:connect() or you can try looking at this example from the MySQLOO Github page.
MySQLOO/database.lua at master · FredyH/MySQLOO · GitHub
Thank you!
But now it does not write any data into the database
You have to remember that MySQLOO is asynchronous. You can't define and variable then add a query and expect it to return a value like on Sqlite. You will have to make a callback function to run the code once your query has completed.
If you have to get the information immediately then you can halt the server while it processes the query but it's not recommended if your connection to the database is poor since all players will time out for a second.
Sorry, you need to Log In to post a reply to this thread.