MTA Team Lpsd Posted December 13, 2016 MTA Team Posted December 13, 2016 (edited) I have the following, which just gets "skin", "cash" and "wasWanted" from the DB function spawnOnLogin () playerName = getPlayerName(source) local findPlayer = dbQuery(db, "SELECT `skin`, `cash`, `wasWanted` FROM player_stuff WHERE name=?", playerName ) local dataresult = dbPoll ( findPlayer, -1 ) if dataresult[1] then local skin = dataresult["skin"] local cash = dataresult["cash"] local wasWanted = dataresult["wasWanted"] end end addEventHandler("onPlayerJoin", getRootElement(), spawnOnLogin) the variables "skin", "cash" and "wasWanted" all come back nil, since I can't work out the right way to get the result. Been looking for ages but can't find the right way to do it, can someone explain to me how I retrieve these values from the query result properly? Thanks Edited December 13, 2016 by LopSided_
iPrestege Posted December 13, 2016 Posted December 13, 2016 (edited) Not sure but you can try this function spawnOnLogin ( ) local dataresult = dbPoll ( dbQuery ( db,'SELECT * FROM `player_stuff` WHERE name=?',getPlayerName ( source ) ),-1 ) if type ( dataresult ) == 'table' and #dataresult == 0 or not dataresult then return end local skin = dataresult [1] ['skin'] local cash = dataresult [1] ['cash'] local wasWanted = dataresult [1] ['wasWanted'] end addEventHandler ( 'onPlayerJoin',root,spawnOnLogin ) @LopSided_ Edited December 13, 2016 by iPrestege typo 1
MTA Team Lpsd Posted December 13, 2016 Author MTA Team Posted December 13, 2016 (edited) 15 minutes ago, iPrestege said: Not sure but you can try this function spawnOnLogin ( ) local dataresult = dbPoll ( dbQuery ( db,'SELECT * FROM `player_stuff` WHERE name=?',getPlayerName ( source ) ),-1 ) if type ( dataresult ) == 'table' and #dataresult == 0 or not dataresult then return end local skin = dataresult [1] ['skin'] local cash = dataresult [1] ['cash'] local wasWanted = dataresult [1] ['wasWanted'] end addEventHandler ( 'onPlayerJoin',root,spawnOnLogin ) @LopSided_ Worked by changing to "dataresult[1]['skin']" etc instead of just "dataresult['skin']" Thanks Edited December 13, 2016 by LopSided_
tosfera Posted December 13, 2016 Posted December 13, 2016 Please do know that this will cause lag if your sql server is working really hard. Lets say.. if a lot of people are logging in at the same time. I would rather use a callback for this, like so: function spawnOnLogin () dbQuery ( function ( queryHandler ) local result = dbPoll ( queryHandler, -1 ); if ( #result > 0 ) then local skin = result [ 'skin' ]; local cash = result [ 'cash' ]; local waswanted = result [ 'waswanted' ]; end end, "SELECT * FROM `player_stuff` WHERE `name` = ?", getPlayerName ( source ) ); end addEventHandler ( "onPlayerJoin", getRootElement(), spawnOnLogin ); Especially since it's onPlayerJoin, you should use callbacks like that. 1
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