Hello. Right now I’m using ESX.TriggerServerCallback function to get data from the database, but I want to make it without using ESX. I know how to pass the data such as different variables etc. from server-side to client-side but I have never tried doing so from the database. Can anyone give me an easy example or link to a script which does that and is not associated with ESX? I really appreciate any help. Thanks.
Isn’t it as easy as just in your query callback triggering the event with the data?
MySQL.Async.fetchAll("SELECT * FROM data", {}, function(result)
TriggerClientEvent("gibdata", source, result)
end)
This
@Shiroyo if you’re still after a working example, DM and I’ll send you an old super embarrassing version of one of my resources that used MySQL, so you can see how to access the retrieved data
RegisterServerEvent('GetPlayerData')
AddEventHandler('GetPlayerData', function()
local identifier = SteamIdentifier()
MySQL.Async.fetchAll(
'SELECT role, class, gender, race FROM pf_profiles WHERE identifier = @identifier',{['@identifier'] = identifier},
function(result)
TriggerClientEvent("GetPlayerData", source, result)
end)
end)
Thanks for reply, really appreciate that. Did I made a mistake here, because this one doesn’t work?
TriggerClientEvent line is triggering an native fatal error in the console. Or do I need to select all of the data from the table instead of selected things?
Waiting for MySQL to be ready
You need to encapsulate your code intoMySQL.ready
to be sure that the mod will be available and initialized before your first request.
GitHub - brouznouf/fivem-mysql-async: MySql Async Library for FiveM
Hmm, there is still the same error. It says the argument is null, but I don’t see why, because identifier is correct for sure.
RegisterServerEvent('GetPlayerData')
AddEventHandler('GetPlayerData', function()
local identifier = SteamIdentifier()
MySQL.ready(function ()
MySQL.Async.fetchAll(
'SELECT role, class, gender, race FROM pf_profiles WHERE identifier = @identifier',{['@identifier'] = identifier},
function(result)
TriggerClientEvent("GetPlayerData", source, result)
end)
end)
end)
That’s not a native, so it must be one of your functions; print it console and check it is not nil
.
“Argument at index 1 was null”, so your source is probably being null because it’s inside an callback
I tried putting string of steamid:mynumbers instead of this function I made to get the player steam identifier and it’s still the same.
Yeah ignore me, @TheIndra makes a much better case
Thanks. Okay, so now I fixed the error in sever side, but now I’m having the error that says data is nil in the client side.
local data
RegisterServerEvent('GetPlayerData')
AddEventHandler('GetPlayerData', function()
local identifier = SteamIdentifier()
MySQL.ready(function ()
MySQL.Async.fetchAll(
'SELECT role, class, gender, race FROM pf_profiles WHERE identifier = @identifier',{['@identifier'] = identifier},
function(result)
data = result
end)
end)
TriggerClientEvent("GetPlayerData", source, data)
end)
And in client side I tried this two:
RegisterNetEvent("GetPlayerData")
AddEventHandler("GetPlayerData", function(data)
CharacterData.UserRole = data[1].role
CharacterData.Class = data[1].class
CharacterData.Gender = data[1].gender
CharacterData.Race = data[1].race
print(CharacterData.Race)
end)
RegisterNetEvent("GetPlayerData")
AddEventHandler("GetPlayerData", function(data)
CharacterData.UserRole = data[1]
CharacterData.Class = data[2]
CharacterData.Gender = data[3]
CharacterData.Race = data[4]
print(CharacterData.Race)
end)
That’s not how callbacks work, now the TriggerClientEvent wil be called before the sql query returns.
Store source in a temp local variable and use that in the callback
Thanks. Now all works like a charm
can you send your right code?
Why not post your working code for others who are searching?
Please can you send the code? I have same problem, and I cant follow your code’s changes!