Jump to content

Account-System (MySQL)


DLmass

Recommended Posts

Hey, I'm currently making myself an nice login system that uses mysql. But I currently ran into some problems that I can't solve..

addEvent("onRegister",true) 
addEvent("onLogin",true) 
  
function resourceStart () 
   handler = dbConnect("mysql","dbname=mta;host=localhost","root","1234567890") 
   if handler then 
       outputServerLog("(Account-System) Connected to the handler server successfully!") 
   else 
       outputServerLog("(Account-System) Connection to the handler server could not be established!") 
   end 
end 
addEventHandler("onResourceStart",getResourceRootElement(),resourceStart) 
  
function playerJoin() 
    setPlayerMuted(source, true) 
end 
addEventHandler ( "onPlayerJoin", getRootElement(), playerJoin ) 
  
function onRegister ( Username , Password ) 
   local sql = dbQuery(handler,"SELECT * FROM PlayerInfo WHERE Username = '"..Username.."'") 
   local result,num_rows = dbPoll(sql, -1) 
   if num_rows == 0 then 
       local encryptedPassword = md5(Password) 
       dbExec(handler,"INSERT INTO PlayerInfo (Username, Password) VALUES ( '"..Username.."','"..encryptedPassword.."')") 
       outputChatBox("You have registered successfully!",source,125,0,0) 
   else 
       outputChatBox("You are already registrered!",source,125,0,0) 
   end 
end 
addEventHandler("onRegister",getRootElement(),onRegister) 
  
function onLogin ( Username , Password ) 
    local encryptedPassword  = md5(Password) 
    local sql = dbQuery(handler,"SELECT * FROM PlayerInfo WHERE Username = '"..Username.."' AND Password = '"..encryptedPassword.."'") 
    local result,num_rows = dbPoll(sql, -1) 
    if num_rows == 1 then 
        outputChatBox("You have successfully logged in!",source,125,0,0) 
        triggerClientEvent(source,"closeLogin",source) 
        spawnPlayer ( source, 0.0, 0.0, 5.0, 90.0, 0 ) 
        setPlayerMuted(source, false) 
        fadeCamera (source, true) 
        setCameraTarget (source, source) 
    else 
        outputChatBox("Your Password or Username is wrong!",source,125,0,0) 
    end 
end 
addEventHandler("onLogin",getRootElement(),onLogin) 
  
function saveAccount () -- Save in the handler 
    local x,y,z = getElementPosition( source ) 
    local i = getElementInterior( source ) 
    local d = getElementDimension( source ) 
    local skin = getPedSkin ( source ) 
    local money = getPlayerMoney ( source ) 
    local health = getElementHealth ( source ) 
    local armor = getPedArmor ( source ) 
    local sql =  dbQuery(handler,"SELECT * FROM `PlayerInfo` WHERE `Username` = '"..Username.."'") 
    if(mysql_num_rows(sql) == 0) then 
        dbQuery( handler, "INSERT INTO PlayerInfo ( `Username` , x, y, z, interior, dimension, skin, money, health, armor, wanted ) VALUES ( '"..Username.."', " .. x .. ", " .. y .. ", " .. z .. "," .. i .. ", " .. d .. "," .. skin .. "," .. money .. ", ".. health ..", ".. armor .." )" ) 
    else 
        res = dbQuery ( handler, "UPDATE `Username` SET x = ".. x ..", y =  ".. y ..", z = ".. z ..", interior = ".. i ..", dimension = ".. d ..", skin = ".. skin ..", money = ".. money ..", health = ".. health ..", armor = ".. armor ..", wanted WHERE `serial` = '"..Username.."'") 
    end 
end 
addEventHandler ( "onPlayerQuit", getRootElement(), saveAccount ) 
  
function loadAccount () -- Load from the handler 
    local result = dbQuery(handler,"SELECT * FROM `PlayerInfo` WHERE `Username` = '"..Username.."'") 
    if result then 
        while true do 
            local row = mysql_fetch_assoc(result) 
            if not row then break end 
            setElementPosition ( source, row.x, row.y, row.z) 
            setElementInterior ( source, row.interior ) 
            setElementDimension ( source, row.dimension ) 
            setPedSkin ( source, row.skin ) 
            setPlayerMoney ( source, row.money ) 
            setElementHealth ( source, row.health) 
            setPedArmor ( source, row.armor ) 
            break 
        end 
    end 
end 
addEventHandler ( "onLogin", getRootElement(), loadAccount ) 

[00:29:38] ERROR: Account-System\server.lua:67: attempt to concatenate global 'U

sername' (a nil value)

Well, I've tried everything.. I'd like to get some help on my way..

Link to comment
addEvent("onRegister",true) 
addEvent("onLogin",true) 
  
function resourceStart () 
   handler = dbConnect("mysql","dbname=mta;host=localhost","root","1234567890") 
   if handler then 
       outputServerLog("(Username-System) Connected to the database server successfully!") 
   else 
       outputServerLog("(Username-System) Connection to the database server could not be established!") 
   end 
end 
addEventHandler("onResourceStart",getResourceRootElement(),resourceStart) 
  
function playerJoin() 
    setPlayerMuted(source, true) 
end 
addEventHandler ( "onPlayerJoin", getRootElement(), playerJoin ) 
  
function onRegister ( Username , Password ) 
   local sql = dbQuery(handler,"SELECT * FROM PlayerInfo WHERE Username = '"..Username.."'") 
   local result,num_rows = dbPoll(sql, -1) 
   if num_rows == 0 then 
       local encryptedPassword = md5(Password) 
       dbExec(handler,"INSERT INTO PlayerInfo (Username, Password) VALUES ( '"..Username.."','"..encryptedPassword.."')") 
       outputChatBox("You have registered successfully!",source,125,0,0) 
   else 
       outputChatBox("You are already registrered!",source,125,0,0) 
   end 
end 
addEventHandler("onRegister",getRootElement(),onRegister) 
  
function onLogin ( Username , Password ) 
    local encryptedPassword  = md5(Password) 
    local sql = dbQuery(handler,"SELECT * FROM PlayerInfo WHERE Username = '"..Username.."' AND Password = '"..encryptedPassword.."'") 
    local result,num_rows = dbPoll(sql, -1) 
    if num_rows == 1 then 
        outputChatBox("You have successfully logged in!",source,125,0,0) 
        triggerClientEvent(source,"closeLogin",source) 
        spawnPlayer ( source, 0.0, 0.0, 5.0, 90.0, 0 ) 
        setPlayerMuted(source, false) 
        fadeCamera (source, true) 
        setCameraTarget (source, source) 
    else 
        outputChatBox("Your Password or Username is wrong!",source,125,0,0) 
    end 
end 
addEventHandler("onLogin",getRootElement(),onLogin) 
  
function saveUsernames ( Username ) -- Save in the handler 
    local x,y,z = getElementPosition( source ) 
    local i = getElementInterior( source ) 
    local d = getElementDimension( source ) 
    local skin = getPedSkin ( source ) 
    local money = getPlayerMoney ( source ) 
    local health = getElementHealth ( source ) 
    local armor = getPedArmor ( source ) 
    local wanted = getPlayerWantedLevel ( source ) 
    local q =  dbQuery(handler,"SELECT * FROM `PlayerInfo` WHERE `Username` = '"..Username.."'") 
    if(mysql_num_rows(q) == 0) then 
        dbQuery( handler, "INSERT INTO PlayerInto ( `Username` , x, y, z, interior, dimension, skin, money, health, armor ) VALUES ( '"..Username.."', " .. x .. ", " .. y .. ", " .. z .. "," .. i .. ", " .. d .. "," .. skin .. "," .. money .. ", ".. health ..", ".. armor .." )" ) 
    else 
        res = dbQuery ( handler, "UPDATE `Username` SET x = ".. x ..", y =  ".. y ..", z = ".. z ..", interior = ".. i ..", dimension = ".. d ..", skin = ".. skin ..", money = ".. money ..", health = ".. health ..", armor = ".. armor .." WHERE `Username` = '"..Username.."'") 
    end 
end 
  
function loadUsernames ( Username ) -- Loading from the handler 
    local result = dbPoll(dbQuery(handler, "SELECT * FROM PlayerInfo WHERE Username = '"..Username.."';"),-1) 
    if result then 
        while true do 
            local row = mysql_fetch_assoc(result) 
            if not row then break end 
            setElementPosition ( source, row.x, row.y, row.z) 
            setElementInterior ( source, row.interior ) 
            setElementDimension ( source, row.dimension ) 
            setPedSkin ( source, row.skin ) 
            setPlayerMoney ( source, row.money ) 
            setElementHealth ( source, row.health) 
            setPedArmor ( source, row.armor ) 
            break 
        end 
    end 
end 
addEventHandler ( "onLogin", getRootElement(), loadUsernames ) 
addEventHandler ( "onPlayerQuit", getRootElement(), saveUsernames ) 
  

I get this error

[01:56:11] ERROR: Account-System\server.lua:70: bad argument #1 to 'mysql_fetch_assoc' (mysqlResult expected, got table)

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