• [MYSQLOO] Not giving a Correct Result.
    2 replies, posted
Hello, I've been having problems trying to get the true/false result if any of the rows exist. Currently, i know this setup of getting results is a bad way of doing so. But, When I localize or not the 'result' variable. It still results into false because of the initializing of result. But, if i add a [CODE] print(result) [/CODE] In between the onSuccess(data) function, it gives me the correct result then later prints it automatically false. I've tried making a special function to handle true/false by returning the result. But, i get the same results to being 'false'. When, i print all the rows out, it prints the correct rows out like it's suppose to. So, it doesn't say its not existing. [CODE] local result = false local query = db:query("SELECT `*` FROM `players` WHERE `steamID` = '".. ply:SteamID() .."' LIMIT 1") function query:onSuccess(data) local row = data[1] for k,v in pairs(row) do print("Rows: " .. v) result = true end end function query:onError(err) print("An error occured while executing the query: " .. err) end query:start() print("Results: " .. tostring(result)) // false [/CODE] If you guys can help, that would be grateful. Thanks.
SQL is asynchronous - which means your print is being ran before onSuccess, that's why it prints false first. [editline]17th June 2016[/editline] Also it's "SELECT *" not "SELECT '*'" [editline]17th June 2016[/editline] also your players shouldn't be in ' ', aswell as steamid ' ' - it's just not needed. [ SELECT * FROM players WHERE steamID = '" .. blah .."' LIMIT 1 ] will work fine.
Ok, so.. I've been trying to place a query within a query, but i wanted to make sure this account/player existed in my database so i can log him/her in without creating a useless new account in my database. Would i do: [CODE] local query = db:query("SELECT * FROM players WHERE steamID = '".. ply:SteamID() .."' LIMIT 1") function query:onSuccess(data) local d = query:getData(); if d ~= nil then print( "creating ".. ply:Nick() .. "'s player data.") local query2 = db:query([[ INSERT INTO `players` (steamID, lastUsername, lastIP) VALUES (']].. ply:SteamID() ..[[', ']].. ply:Nick() ..[[', ']].. ply:IPAddress() ..[['); ]]) query2.onSuccess = function(_, data) ply:ChatPrint("Created player data...") end query2.onError = function(_, err, sql) ply:ChatPrint("There was an error creating your data...") end query2:start(); else print( ply:Nick() .. " logged back in." ) ply:ChatPrint("Hello, again.") end end function query:onError(err) print("An error occured while executing the query: " .. err) end query:start() [/CODE] [editline]17th June 2016[/editline] Fixed it.. I was not reading the data correctly. Thank you for trying to help. Free to use this code, if you guys need assistance with a simple login system. [CODE] local query = db:query("SELECT `*` FROM `players` WHERE `steamID` = '".. ply:SteamID() .."' LIMIT 1") query.onSuccess = function(q, data) PrintTable( data ) print("Checking Data: " .. tostring(checkdata(data))) if !checkdata(data) then print( "creating ".. ply:Nick() .. "'s player data.") local query2 = db:query([[ INSERT INTO `players` (steamID, lastUsername, lastIP) VALUES (']].. ply:SteamID() ..[[', ']].. ply:Nick() ..[[', ']].. ply:IPAddress() ..[['); ]]) query2.onSuccess = function(_, data) ply:ChatPrint("Created player data...") end query2.onError = function(_, err, sql) ply:ChatPrint("There was an error creating your data...") end query2:start(); else print( ply:Nick() .. " logged back in." ) ply:ChatPrint("Hello, again.") end end query.onError = function(_, err) print("Error occured...") print(err) end query:start() [/CODE]
Sorry, you need to Log In to post a reply to this thread.