Jump to content

SQL if exist


Jimmy.

Recommended Posts

I check or row is exist, but always got " Wrong username" even username is in database. Where can be a mistake?

function loginHandler(username,password) 
    local query = dbQuery(handler,"SELECT * FROM accounts WHERE username=?", username) 
    local result = dbPoll( query, -1 ) 
    if result == true then 
        local query = dbQuery(handler,"SELECT * FROM accounts WHERE username=? AND password=?",username,password) 
        local result = dbPoll( query, -1 ) 
        if result 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("SubmitLogin",true) 
addEventHandler("SubmitLogin",root,loginHandler) 
  

Link to comment

dbPoll doesn't return boolean value.

function loginHandler(username,password) 
    local query = dbQuery(handler,"SELECT * FROM accounts WHERE username=?", username) 
    local result = dbPoll( query, -1 ) 
    if result then 
        local query = dbQuery(handler,"SELECT * FROM accounts WHERE username=? AND password=?",username,password) 
        local result = dbPoll( query, -1 ) 
        if result 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("SubmitLogin",true) 
addEventHandler("SubmitLogin",root,loginHandler) 

Link to comment
dbPoll doesn't return boolean value.
function loginHandler(username,password) 
    local query = dbQuery(handler,"SELECT * FROM accounts WHERE username=?", username) 
    local result = dbPoll( query, -1 ) 
    if result then 
        local query = dbQuery(handler,"SELECT * FROM accounts WHERE username=? AND password=?",username,password) 
        local result = dbPoll( query, -1 ) 
        if result 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("SubmitLogin",true) 
addEventHandler("SubmitLogin",root,loginHandler) 

But now always show: "Welcome to My Server." Or wrong username or no.

Link to comment

Oh, use this:

function loginHandler(username,password) 
    local query = dbQuery(handler,"SELECT * FROM accounts WHERE username=?", username) 
    local result = dbPoll( query, -1 ) 
    if #result == 1 then 
        local query = dbQuery(handler,"SELECT * FROM accounts WHERE username=? AND password=?",username,password) 
        local result = dbPoll( query, -1 ) 
        if #result == 1 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("SubmitLogin",true) 
addEventHandler("SubmitLogin",root,loginHandler) 

Link to comment

Then you're entering wrong username. Debug the code like:

function loginHandler(username,password) 
    local query = dbQuery(handler,"SELECT * FROM accounts WHERE username=?", username) 
    local result = dbPoll( query, -1 ) 
    outputChatBox(#result) 
    if #result == 1 then 
        local query = dbQuery(handler,"SELECT * FROM accounts WHERE username=? AND password=?",username,password) 
        local result = dbPoll( query, -1 ) 
        if #result == 1 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("SubmitLogin",true) 
addEventHandler("SubmitLogin",root,loginHandler) 

Also, don't forget to use dbFree.

Link to comment

Maybe. Output username and then compare to the row in the table.

function loginHandler(username,password) 
    local query = dbQuery(handler,"SELECT * FROM accounts WHERE username=?", username) 
    outputChatBox(username) 
    local result = dbPoll( query, -1 ) 
    outputChatBox(#result) 
    if #result == 1 then 
        local query = dbQuery(handler,"SELECT * FROM accounts WHERE username=? AND password=?",username,password) 
        local result = dbPoll( query, -1 ) 
        if #result == 1 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("SubmitLogin",true) 
addEventHandler("SubmitLogin",root,loginHandler) 

Link to comment
Maybe. Output username and then compare to the row in the table.
function loginHandler(username,password) 
    local query = dbQuery(handler,"SELECT * FROM accounts WHERE username=?", username) 
    outputChatBox(username) 
    local result = dbPoll( query, -1 ) 
    outputChatBox(#result) 
    if #result == 1 then 
        local query = dbQuery(handler,"SELECT * FROM accounts WHERE username=? AND password=?",username,password) 
        local result = dbPoll( query, -1 ) 
        if #result == 1 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("SubmitLogin",true) 
addEventHandler("SubmitLogin",root,loginHandler) 

outputChatBox(username) here it is okay, what i wrote, output show the same.

I send PM my all clientside file.

Link to comment

There is no problem in client-sided file.

Try:

function loginHandler(username,password) 
    local query = dbQuery(handler,"SELECT * FROM accounts WHERE username=`??`", username) 
    local result = dbPoll( query, -1 ) 
    outputChatBox(#result) 
    if #result == 1 then 
        local query = dbQuery(handler,"SELECT * FROM accounts WHERE username=`??` AND password=`??`",username,password) 
        local result = dbPoll( query, -1 ) 
        if #result == 1 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("SubmitLogin",true) 
addEventHandler("SubmitLogin",root,loginHandler) 

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