MySQL always no result

So I’ve been trying to create a custom system (kinda lika a garage system, but different) for my server.
As I got to the part where I display the player all vehicles he ownes I need to create a server callback where the script gets all vehicles from the database using the license. The problem is, that the result is always nil - even if there are saved vehicles. I also see the saved vehicles displayed in the database, so that works.

Server-Side Code:

QBCore.Functions.CreateCallback("emergsaver:server:GetUserVehicles", function(source, cb)
	local src = source
	local pData = QBCore.Functions.GetPlayer(src)
	exports.oxmysql:execute('SELECT * FROM saved_emergency_vehicles WHERE license = ?', pData.PlayerData.license, function(result)
		if result[1] then
			print('PosRes')
			cb(result)
		else
			print('NegRes')
			cb(nil)
		end
	end)
end)

Client-Side Code:

RegisterNetEvent('emergsaver:client:VehicleList', function()
	QBCore.Functions.TriggerCallback('emergsaver:server:GetUserVehicles', function(result)
		if result == nil then
			QBCore.Functions.Notify("Keine gespeicherten Fahrzeuge", "error", 5000)
		else
			local MenuPublicGarageOptions = {
				{
					header = "Polizeiwagengarage",
					isMenuheader = true
				},
			}
			for k, v in pairs(result) do
				vname = GetDisplayNameFromVehicleModel(v.model)
				liv = v.livery
				local livtext
				if liv == 1 then
					livtext = "BCSO"
				elseif liv == 2 then
					livtext = "SAST"
				elseif liv == 3 then
					livtext = "Unmarked"
				elseif liv == 4 then
					livtext = "LSSD"
				elseif liv > 4 then
					livtext = "LSPD"
				end
				
				MenuPublicGarageOptions[#MenuPublicGarageOptions+1] = {
					header = vname.." [".. v.plate.."]",
					txt = "Livery: " .. livtext,
					params = {
						event = "emergsaver:client:spawnVeh",
						args = v,
					}
				}
				exports['qb-menu']:openMenu(MenuPublicGarageOptions)
			end
			MenuPublicGarageOptions[#MenuPublicGarageOptions+1] = {
                header = "⬅ Schließen",
                txt = "",
                params = {
                    event = "qb-menu:closeMenu",
                }
            }
		end
	end)
end)

I copied the code from another working system, but it just doens’t work for me.
There are no errors displayed.

~ Paul

First, try to add the license variable in a table, like this: (not sure oxmysql has formatting for single vars, which is why I am suggesting this):

exports.oxmysql:execute('SELECT * FROM saved_emergency_vehicles WHERE license = ?', {pData.PlayerData.license}, function(result)
		if result[1] then
			print('PosRes')
			cb(result)
		else
			print('NegRes')
			cb(nil)
		end
	end)

If that doesn’t fix it, try to run the SQL directly in your db, maybe the license wasn’t saved correctly.

1 Like

That’s what I would suggest. I am pretty sure that’s the solution.
When providing variables for your SQL queries, you should be putting them inside a table, not just as a single passed variable.

So change;
WHERE license = ?', pData.PlayerData.license, function(result)
to;
WHERE license = ?', { pData.PlayerData.license }, function(result)
Just like the first response suggested and if it works, mark their post as the solution. :crossed_fingers: