Hello!
I am trying to find out how can I send data from database to server. For example:
I have a table where I store some random numbers on server. Every 5 seconds there is +5 in that collumn (I managed to do that, so saving to database works). Now when server restarts I want to get that data to server. It always says 0, but in database is some number
Well it depends what you are referring to. If you are referring to cash/bank/ ect. then you need to assign the value to the player from the database. As far as retrieving data you would simply use something like:
MySQL.Async.fetchAll(âSELECT * FROM users WHERE identifier = @identifierâ, { --gets all columns where the identifier = steamID
[â@identifierâ] = identifier --defines it as a variable
}, function(result)
print(result[1].identifier) --prints the result
local identifier= result[1].identifier --redefines the variable to a more readable standard
Okay and then how to send it to client so player can read it when he goes to marker?
I dont know much about using databases with .lua, Iâm more used to PHP⌠I doubt this would help but in the off chance it does, let me know⌠Im curious.
Server.lua :
MySQL.Sync.fetchAll('SELECT * FROM tabelname WHERE columnname = @number', {
['@number'] = whatever-you-use-to-add-5-values
})
Youâd have to figure out how and where to utilize this, but on another note, howâd you get it to send to a database? Iâve been to send 911 calls to my DB for my CAD system to use but I cant even get it to insert to a table.
~~ Merry Christmas, Happy Holidays
Okay so to store in DB from client you have to trigger server event
For example:
client.lua
if IsControlJustReleased(0, Keys['E']) then
TriggerServerEvent("resource:SaveNumber", Number)
end
server.lua
RegisterServerEvent('resource:SaveNumber')
AddEventHandler('resource:SaveNumber', function (Number) -- This Number is defined in Client
MySQL.Sync.execute("UPDATE table SET number=@number WHERE row= 'row1'",
{
['@number'] = Number
})
end)
So this works for now. Next what I want to do is to get that saved Number from Database and send it to client but I cant do it.
All DB code needs to be done in the server script, then sent to client script.
Can you show us what you have and what youâre trying to do.
Ok⌠So I want to make products system. I have database table called products and there I have 2 columns. One is faction name and other is product number⌠When player presses E it gives random amount of products to that faction.
Client.lua
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
if IsControlJustReleased(0, Keys['E']) then
TriggerServerEvent("esx_products:SaveProduct", Products)
end
end
end)
Products are defined by
local Products = 0
and then later it gives them like:
Products = Products + math.random(0,5)
Server.lua
RegisterServerEvent('esx_products:SaveProduct')
AddEventHandler('esx_products:SaveProduct', function (Products)
MySQL.Sync.execute("UPDATE products SET amount=@amount WHERE faction = 'faction1'",
{
['@amount'] = Products
})
end)
So this works⌠It saves it normaly in Database. But I want to make that when player presses E in marker, it fetches amount from database and writes that amount in Chat or in TextDraw⌠Now it says there are 0 products, but in database it says right amount, so my fetching from server.lua to client.lua doesnt work
Rule of thumb, you should always wrap your code in a ready, see here for details: GitHub - brouznouf/fivem-mysql-async: MySql Async Library for FiveM
Can you show us that code as well please?
Here you go:
Server.lua
RegisterServerEvent('esx_products:GetProduct')
AddEventHandler('esx_products:GetProduct', function (DBProducts)
MySQL.Async.fetchAll('SELECT amount FROM products WHERE faction = "faction1"',
{
['@amount'] = DBProducts
},
end)
Iâm confused as to what this does. You have DBProducts which is passed to the event, then you select a column from your table, then you define @amount inside your array parameters even though you donât use it in you query, then you donât do anything with the data you collect? Plus itâs not wrapped like I mentioned earlier?
I think you need to go have a read of the documentation, because youâre just using it incorrectly.
Will it be problem for you if you write me an example of esx_products:GetProducts from database?
I am trying to re-read through this⌠and I am struggling to understand.
@Christopher_Lauer
I will try to explain you. There is a faction on server. Every five minutes that faction produces 10 cigarettes. So normally that number (10 cigarettes) saves to Database, right? When player enters marker near that factory and press E it shows him how much cigarettes that factory produced. To show that you need to fetch or grab or take from database number of cigarettes that factory produced. SO: I know how to save number of cigarettes to database, but I dont know how to GRAB/FETCH/GET them from database when player press E.
Ok, I understand. Now, is the player to receive these cigarettes, or simply just display? @DonIvanda
It sounds like you need to find the player faction first. SoâŚ
I havenât tested, but something like this should work. Dependent on how your tables are structured.
--CLIENT--
Citizen.CreateThread(function()
while true do
if IsControlJustReleased(0, Keys['E']) then
TriggerServerEvent('esx_products:getCigarettes')
end
end
end)
--SERVER--
RegisterServerEvent('esx_products:getCigarettes')
AddEventHandler('esx_products:getCigarettes', function(product)
local xPlayer = ESX.GetPlayerFromId(source)
local identifier = GetPlayerIdentifiers(source)[1]
--below will select the faction the user is associated with
MySQL.Async.fetchAll('SELECT faction FROM users WHERE identifier = @identifier', {
['@faction'] = faction,
['@identifier'] = identifier
}, function(result)
--will print the faction associated with the user
print(result[1].faction)
--assigns the variable 'faction' to whatever the users faction is associated with
local faction = result[1].faction
-- selects cigarettes from products_table where the products faction = the user faction
MySQL.Async.fetchAll('SELECT cigarettes FROM `products_table` WHERE `faction` = @faction', {
['@faction'] = faction
}, function(result)
print(result[1])
--product_count would be the variable assigned to howmany cigs are in database
product_count = result[1]
end)
end)
end)
Thank you⌠I will test it⌠I need it to show to player⌠Every player has to see same value
To get it to display to player, you will need to setup a GUI for that. Then send the info from server to client. But you should see the counts in your console at that point. But since the variable is already created at this point is smooth sailing. Or show it as an alert/menu item
What i sent you should show the same value to every player, as long as they are in the same faction.
Hi this is a little off the topic too but not some much I was having the same issue with my QB-multicharacter so I added the Lua and JSON once I did that I begin to get this error how do I start to fix it I thought if I created a table called what it was asking for it would fix it but it didnât so no Iâm kinda stuck and not really sure how to fix it what do I change my SELECT * from users were identifier =? Iâm not sure what the? suppose to be