Jump to content

SQL Login system


Jimmy.

Recommended Posts

Hello, what's wrong with this code? How to make, that doesn't show error about SQL nil? http://prntscr.com/7s2rbp.

local handler = exports.SQL:connect() 
function loginHandler(usernametext,passwordtext) 
    local query = dbQuery(handler,"SELECT * FROM accounts WHERE Username=?", usernametext) 
    if (query) then 
        local query = dbQuery(handler,"SELECT * FROM accounts WHERE Username=? AND Password=?",usernametext,passwordtext) 
        if (query) then 
            if (client) then 
                spawnPlayer(client, 1959.55, -1714.46, 10) 
                fadeCamera(client, true) 
                setCameraTarget(client, client) 
                outputDebugString("Welcome to My Server.") 
            end 
        else 
            outputDebugString("Wrong password.") 
        end 
    else 
        outputDebugString("Wrong username.") 
    end 
end 
addEvent("submitLoginStepTwo",true) 
addEventHandler("submitLoginStepTwo",root,loginHandler) 

local HOST = "localhost" 
local USERNAME = "root" 
local PASSWORD = "" 
local DATABASE = "mta" 
local connection = false 
  
function connect() 
    connection = dbConnect("mysql","dbname="..DATABASE..";host="..HOST..";",USERNAME,PASSWORD) 
end 
addEventHandler("onResourceStart",resourceRoot,connect) 

Link to comment

An easy way to achieve what you wanted is this.

local HOST = "localhost" 
local USERNAME = "root" 
local PASSWORD = "" 
local DATABASE = "mta" 
local connection = false 
  
function getConnection() 
    return connection 
end 
  
function connect() 
    connection = dbConnect("mysql","dbname="..DATABASE..";host="..HOST..";",USERNAME,PASSWORD) 
end 
addEventHandler("onResourceStart",resourceRoot,connect) 

Just add an export for the getConnection, and change the first line of the other code to

local handler = exports.SQL:getConnection() 

Link to comment
An easy way to achieve what you wanted is this.
local HOST = "localhost" 
local USERNAME = "root" 
local PASSWORD = "" 
local DATABASE = "mta" 
local connection = false 
  
function getConnection() 
    return connection 
end 
  
function connect() 
    connection = dbConnect("mysql","dbname="..DATABASE..";host="..HOST..";",USERNAME,PASSWORD) 
end 
addEventHandler("onResourceStart",resourceRoot,connect) 

Just add an export for the getConnection, and change the first line of the other code to

local handler = exports.SQL:getConnection() 

Thanks, it works, but I have another mistake. Whatever username or password write always fit. Where to look for errors?

Link to comment

dbQuery's documentation on the wiki; Returns a query handle unless the connection is incorrect, in which case it return false.

You're only checking if you got something, which is true. You got a query handle, therefore all your accounts 'known'. You should use DbPoll to see if you actually got a row returned.

Link to comment
dbQuery's documentation on the wiki; Returns a query handle unless the connection is incorrect, in which case it return false.

You're only checking if you got something, which is true. You got a query handle, therefore all your accounts 'known'. You should use DbPoll to see if you actually got a row returned.

Thanks.

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...