Jump to content

mySQL Not Saving


Lloyd Logan

Recommended Posts

Posted

server = dbConnect ( "mysql", "dbname=servermta;host=127.0.0.1","root")

dbExec(server, "CREATE TABLE IF NOT EXISTS accounts (serial TEXT NOT NULL, money INT NOT NULL)")

  
function submitReg() 
    firstserial = getPlayerSerial(source) 
    moneys = getPlayerMoney(source) 
    local data = dbPoll(dbQuery(server, "SELECT money FROM accounts WHERE serial = ?", firstserial), -1) 
    if data and type (data) == "table" and #data > 0 then 
    dbExec( server, "UPDATE `accounts` SET `money`=`"..moneys.."`"  ) 
    else 
    dbQuery ( server, "INSERT INTO accounts (serial, money) VALUES (?, ?)", tostring (theserial), tostring (themoney) ) 
    end 
end 
addEventHandler( "onPlayerQuit", root, submitReg ) 
     
     
exports.scoreboard:addScoreboardColumn("Money") 
  
function setMoney() 
    local Serial = getPlayerSerial(source) 
    local data = dbPoll(dbQuery(server, "SELECT money FROM accounts WHERE serial = ?", Serial), -1) 
    if data and type(data) == "table" then 
        setPlayerMoney(source, data[1]["money"]) 
        setElementData(source, "Money", data[1]["money"]) 
    else 
    outputChatBox("You have not previously logged in!", getRootElement(), 255, 0, 0) 
    end 
 end 
addEventHandler("onPlayerJoin", root, setMoney)Why does this save the serial as nil and money as 0? 
  

Posted
What do you mean by "save the serial as nil"? the setMoney function is meant to load the money, so how can it "save the serial"?

Sorry there was some blatant mistakes in that code!

I'm now able to save the money when the player quits, and load it when they join, so now I am stuck on how to update it if their info is already in the table!

function submitReg() 
    firstserial = getPlayerSerial(source) 
    moneys = getPlayerMoney(source) 
    local data = dbPoll(dbQuery(server, "SELECT money FROM accounts WHERE serial = ?", firstserial), -1) 
    if data and type (data) == "table" and #data > 0 then 
    dbExec( server, "UPDATE accounts SET money=`"..moneys.."` WHERE serial = '"..firstserial.."'"   ) 
    else 
    dbQuery ( server, "INSERT INTO accounts (serial, money) VALUES (?, ?)", tostring (firstserial), tostring (moneys) ) 
    end 
end 
addEventHandler( "onPlayerQuit", root, submitReg ) 

Posted
function submitReg ( ) 
    local firstserial = getPlayerSerial ( source ) 
    local moneys = getPlayerMoney ( source ) 
    local data = dbPoll ( dbQuery ( server, "SELECT money FROM accounts WHERE serial = ?", firstserial ), - 1 ) 
    if ( type ( data)  == "table" and #data > 0 ) then 
        dbExec ( server, "UPDATE accounts SET money = ? WHERE serial = ?", moneys, firstserial ) 
    else 
        dbQuery ( server, "INSERT INTO accounts ( serial, money ) VALUES ( ?, ? )", tostring ( firstserial ), tostring ( moneys ) ) 
    end 
end 
addEventHandler ( "onPlayerQuit", root, submitReg ) 

Posted

You should use dbExec for INSERT as you aren't selecting anything, therefore there will not be any return and you will not have to use dbFree nor dbPoll.

If you used dbQuery for INSERT, you will also need to use dbFree as this showed in wiki example.

I don't know what will happen if you didn't used dbFree, but i think the database could be locked or something.

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