MYSQL Select returns nil

I have been at this for a total of 10 hours now split into two days. Im making an account system, its really my first time making one and my account saves just fine. I just want the script to check if the user name already exist first before it saves the account. I made a command to select the user from the db and it just returns nil, at one point I got it return something but it was something like “table: 00045654”. What am I doing wrong to make this return nil?

Server

local userDB = ""

RegisterCommand("save", function(source, args)
	local argString = table.concat(args, " ")
	
	if userDB == nil then
		MySQL.Async.fetchAll("INSERT INTO accounts (id, user, money, level, xp) VALUES(@identifier, @name, @money, @level, @xp)", 
			{['@identifier'] = GetPlayerIdentifiers(source)[1], ["@name"] = GetPlayerName(source), ["@money"]  = 0, ["@level"] = 1, ["@xp"] = 0}
		)
		--notify("~g~Account registered!")
		print ("Registered")
	else 
		--notify("Account already exist")
		print("AC exist already")
	end
	
end)

RegisterCommand("check", function(source)
	userDB = MySQL.Async.fetchAll("SELECT user FROM accounts WHERE id = @id", 
		{["@id"] = GetPlayerIdentifiers(source)[1]}
	)
	--notify("~g~Accounts name is"..userDB..".")
end)

RegisterCommand("dbname", function(source)
	local playerID = GetPlayerIdentifiers(source)[1]
	print(userDB)
end)

I tried it with usernames and couldn’t get a string for nothing. So I tried returning the identifier and that returns nil too.

I suggest reading over the mysql-async documentation as you’re not doing several things correctly.

You would use Async.Execute to do the INSERT and your fetchAll should have a function with the results you’re trying to get, then you need to do results.MyColumn to get the desired results. In this case results.id would get you the id number when the commands have been fixed.

1 Like

Here is an example of one my my queries:

MySQL.ready(function ()
	MySQL.Async.fetchAll('SELECT DmvTest, mugshotimg, PorteArma, CarInsurance FROM vrp_users WHERE id = @user', {['@user'] = user_id}, function(result)
		local insurance = result[1].CarInsurance
		local driversl = result[1].DmvTest
		local firearml = result[1].PorteArma
		user.mugshoturl = result[1].mugshotimg
		if type == "driver" then
			if driversl == "Passed" then
				type = "driver"
				show = true
			elseif driversl == "Required" then
				type = nil
				show = true
			end
		elseif type == "weapon" then
			if firearml == "Passed" then
				type = "weapon"
				show = true
			elseif firearml == "Required" then
				type = nil
				show = true
			end
		else 
			show = true
		end
		if show then
			local array = {user = user,licenses = insurance}
			if type == nil then
				type = null
			end
			TriggerClientEvent('idcard:open', target, array, type)
		end
	end)
end)

Thanks for the reply, I figured the fetchAll(INSERT) not correct but I was watching a video and the dude typed and it worked for I mirrored that but ive fixed it now. Thanks for that.

I updated my script and added a function but now it wont print anything and its not throwing an error

RegisterCommand("check", function(source)
	MySQL.Async.fetchAll('SELECT user FROM accounts WHERE user = @username', {['@username'] = GetPlayerName(source)}, function(result)
		local userName = result[1].user
		print("Account dataname is", userName)
	--notify("~g~Accounts name is"..userDB..".")
	end)
end)

Hold that thought! It isn’t printing in the game console for whatever reason but it did return the right name in the window console

That’s because it’s server-side. You will not see it print in the client console.

I see why it was printing anything in the first place, I was trying to print a var is forgot to assign.

So you have solved my problem, max appreciation man. But now that im trying to throw everything together I have bumped into what should be my last step to this command. Its getting the username from the DB and printing it correctly but for some reason its trying to insert a new table regardless of my IF statement.

RegisterCommand("save", function(source, args)
	local argString = table.concat(args, " ")
	local playerName = GetPlayerName(source)
	local userName = ""
	
	MySQL.Async.fetchAll('SELECT user FROM accounts WHERE user = @username', {['@username'] = GetPlayerName(source)}, function(result)
		userName = result[1].user
		print("Account dataname is", userName)
	--notify("~g~Accounts name is"..userDB..".")
	end)
	
	if userName ~= playerName then
		MySQL.Async.execute("INSERT INTO accounts (id, user, money, level, xp) VALUES(@identifier, @name, @money, @level, @xp)", 
			{['@identifier'] = GetPlayerIdentifiers(source)[1], ["@name"] = GetPlayerName(source), ["@money"]  = 0, ["@level"] = 1, ["@xp"] = 0}
		)
		--notify("~g~Account registered!")
		print ("Registered")
	elseif userName ==  playerName then
		--notify("Account already exist")
		print("AC exist already", userName)
	end
	
end)

It’s because it doesn’t know what userName is in your if statement, the code should look like this:

RegisterCommand("save", function(source, args)
	local argString = table.concat(args, " ")
	local playerName = GetPlayerName(source)
	local userName = ""
	
	MySQL.Async.fetchAll('SELECT user FROM accounts WHERE user = @username', {['@username'] = GetPlayerName(source)}, function(result)
		userName = result[1].user
		print("Account dataname is", userName)
		--notify("~g~Accounts name is"..userDB..".")
		if userName ~= playerName then
			MySQL.Async.execute("INSERT INTO accounts (id, user, money, level, xp) VALUES(@identifier, @name, @money, @level, @xp)", 
				{['@identifier'] = GetPlayerIdentifiers(source)[1], ["@name"] = GetPlayerName(source), ["@money"]  = 0, ["@level"] = 1, ["@xp"] = 0}
			)
			--notify("~g~Account registered!")
			print ("Registered")
		elseif userName ==  playerName then
			--notify("Account already exist")
			print("AC exist already", userName)
		end
	end)
end)

You can nest queries inside each other, that’s how you pass the data down to them.

Thank works perfect. I understand exactly where I went wrong both times. Max appreciation man. I can go to sleep now haha.

Trust me I know how frustrating it is trying to learn this stuff on your own. I’ve spent countless hours testing and re-reading documentation trying to get things to work, so I’m glad it’s working for you now :slight_smile:

1 Like