Hello, I try to get a value in the DB but I’m not able to get the result, can someone tell me what is my mistake please ? I don’t see it.

RegisterServerEvent('Keep_cool:getStatForce')
AddEventHandler('Keep_cool:getStatForce', function()
  local _source = source
  local xPlayer = ESX.GetPlayerFromId(_source)
  
  MySQL.Async.execute('SELECT * FROM users WHERE identifier = "@identifier"',
    {
	  ['@identifier'] = xPlayer.identifier,
    },
    function(result)
		local user = result[1]
	    local force = user['force']
		TriggerClientEvent('Keep_cool:setStatForce', force)
	end)
  
end)

RegisterServerEvent('Keep_cool:getStatEndurance')
AddEventHandler('Keep_cool:getStatEndurance', function()
  local _source = source
  local xPlayer = ESX.GetPlayerFromId(_source)
  
  MySQL.Async.execute('SELECT * FROM users WHERE identifier = "@identifier"',
    {
	  ['@identifier'] = xPlayer.identifier,
    },
    function(result)
	    local user = result[1]
	    local endurance = user['endurance']
		TriggerClientEvent('Keep_cool:setStatEndurance', endurance)
	end)
  
end)

Whats the error message you’re receiving?

I receive this:
Screenshot_47

result is null due to a MySQL error, so you are referencing a value that does not exist. Try removing the quotes around “identifier” in your MySQL query string. So something like this:

MySQL.Async.execute('SELECT * FROM users WHERE identifier = @identifier',
    {
	  ['@identifier'] = xPlayer.identifier,
    },
    function(result)
		local user = result[1]
	    local force = user['force']
		TriggerClientEvent('Keep_cool:setStatForce', force)
	end)

In addition, you could reduce the code to one function to get both stats…

RegisterServerEvent('Keep_cool:getStats')
AddEventHandler('Keep_cool:getStats', function()
  local _source = source
  local xPlayer = ESX.GetPlayerFromId(_source)
  
  MySQL.Async.execute('SELECT * FROM users WHERE identifier = @identifier',
    {
	  ['@identifier'] = xPlayer.identifier,
    },
    function(result)
       --This if statement will prevent error is result is nil
       if result ~= nil then 
		    local user = result[1]
            TriggerClientEvent('Keep_cool:setStatEndurance', user['endurance'])
		    TriggerClientEvent('Keep_cool:setStatForce', user['force'])
        end
	end)
end)

And, lastly, ensure you are getting the ESX shared object prior to using ESX functions by having this on the top of your script:

ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
1 Like

Hello I have tried what you are saying but it still don’t work, and yes I have the ESX shared object at the start of my server.lua :confused:

Screenshot_50

My new code:

The error:
Screenshot_48

PS: I don’t understand how it enter in the result ~= nil if the result return nothing, because when I have printed it, it return 0 it’s strange.

You still have quotes around @identifier in the MySQL query.

I have try without and with, but same error, and in PhpMyAdmin, the request work with the quotes this is why I prefer to see them in the code :stuck_out_tongue:

Quotes work on unprepared statements.
It is returning a number called and not null, rereading the error. Which is odd.

When the result is nil, if I print it’s supposed to show what in the terminal ?

It will just say nil. Add print(tostring(result)) to make it safe to return as a string. I think result may be 0 as “false” since the query failed.

I added print(tostring(result)) in my code before user = result[1] and it still print the number 0

Then the query isn’t working properly. Have you removed the quotes?

Yes I have removed it, do you think this is because the result is empty ?

How are you triggering this event?

Screenshot_52

It all looks fine. I’m not sure at this point. Next time I’m at my computer I’ll try to recreate.


When the player press a key too

Before the query, can you print(tostring(xPlayer.identifier)) ?

Screenshot_54