Jump to content

MySQL database query results type of userdata


Geo

Recommended Posts

Hello, I don't know why, but my script is returning the data sent by my MySQL database as userdata type instead of string, this prevents me from being able to compare the data prompted by the user with the data in the Database.

Log In server:
 

function PlayerJoin()
    triggerClientEvent ("openLoginGUI", resourceRoot, true)
end

function sendData(userLogin, userPassword)
    outputServerLog("Data sent")
    local userLogin = userLogin:gsub("%\n", "")
    local userPassword = userPassword:gsub("%\n", "")
    outputServerLog(userLogin)
	local results = exports.sql:_Query('SELECT * FROM users WHERE playerAccount=? and playerPass=?', userLogin, userPassword)
	--local results = exports.sql:_Query('SELECT * FROM users')
	outputServerLog(type(results))
end

addEvent("receiveDataFromClient", true)

addEventHandler("receiveDataFromClient", resourceRoot, sendData)
addCommandHandler("logintest", PlayerJoin)

 
Log In Client:

 

function login_panel()
    outputConsole("a a a")
    showCursor(true)

    login_box = guiCreateWindow(0.39, 0.29, 0.21, 0.43, "", true)
    guiWindowIsSizable(login_box, false)

    user_input = guiCreateMemo(0.16, 0.15, 0.66, 0.13, "", true, login_box)
    password_input = guiCreateMemo(0.16, 0.30, 0.66, 0.13, "", true, login_box)
    big_button = guiCreateButton(0.17, 0.47, 0.43, 0.11, "", true, login_box)
    small_button = guiCreateButton(0.68, 0.47, 0.15, 0.12, "", true, login_box)
end

function click()
    if source == big_button then
        local user = guiGetText(user_input)
        local password = guiGetText(password_input)
        outputConsole(user)
        outputConsole(password)
        triggerServerEvent("receiveDataFromClient", resourceRoot, user, password)
    end
end

addEventHandler("onClientGUIClick", root, click) 
addEvent("openLoginGUI", true)
addEventHandler("openLoginGUI", resourceRoot, login_panel)


SQL Server Query Function:

 

connection = dbConnect("mysql", "dbname=database;host=127.0.0.1;port=3306;","admin","admin")

function _QueryResult(queryHandle)
    local results = dbPoll(queryHandle, 0)
    return results
end

function _Query( ... )
    if connection then
        query = dbQuery(_QueryResult, connection, ...)
        return query
    else
        outputServerLog("DB: Tried to fetch query but failed.")
        return false
    end
end


 

Edited by Geo
Link to comment
  • Moderators
50 minutes ago, Geo said:

Hello, I don't know why, but my script is returning the data sent by my MySQL database as userdata type instead of string, this prevents me from being able to compare the data prompted by the user with the data in the Database.

You can't retrieve async data with a blocking method.

 

Blocking/sync

function _Query( ... )
    if connection then
        local queryHandle = dbQuery( connection, ...)
        return  dbPoll(queryHandle, -1)
    else
        outputServerLog("DB: Tried to fetch query but failed.")
        return false
    end
end

 

Non-blocking/Async

function _Query(callBackFunctionName ... )
	local sourceResource_ = sourceResource
	if connection then
		dbQuery(
			function (queryHandle)
				local results = dbPoll(queryHandle, 0)
				call ( sourceResource_, callBackFunctionName, results )
			end, connection, ...
		)
		return true
	else
		outputServerLog("DB: Tried to fetch query but failed.")
		return false
	end
end

 

 

 

Edited by IIYAMA
  • Thanks 1
Link to comment
function sendData(userLogin, userPassword)
    outputServerLog("Data sent")
    local userLogin = userLogin:gsub("%\n", "")
    local userPassword = userPassword:gsub("%\n", "")
    outputServerLog(userLogin)
	local qh = exports.sql:_Query('SELECT * FROM users WHERE playerAccount=? and playerPass=?', userLogin, userPassword)
	local results = qh and exports.sql:_QueryResult(qh)
	if type(results) == "table" and #results > 0 then
		outputServerLog(results[1]["playerAccount"])
		outputServerLog(results[1]["playerPass"])
	end
end

 

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...