Jump to content

dbQuery Result?


Lpsd

Recommended Posts

  • Administrators

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 by LopSided_
Link to comment

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 by iPrestege
typo
  • Like 1
Link to comment
  • Administrators
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']" :P

Thanks

Edited by LopSided_
Link to comment

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.

  • Like 1
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...