Jump to content

MySQL


Lloyd Logan

Recommended Posts

I was looking at someones script and saw this;

local result = mysql_query ( database ,"SELECT * FROM `accounts` WHERE `serial` = '"..serial.."'") 
    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) 

So in order for him to use row.blah blah, he used mysql_query then local row = mysql_fetch_assoc

My problem is, i'm using dbQuery, so how would i create one equal to

  
local row = mysql_fetch_assoc(result) 
            if not row then break end 
            setElementPosition ( source, row.x, row.y, row.z) 

except for dbQuery

Thanks!

Link to comment
local query = dbQuery ( handler, "SQL STRING HERE" ) 
local data = dbPoll  ( query, - 1 ) -- -1 = wait for the result, freezes server until it has the result. 
if ( type ( data ) == "table" ) then 
    print ( data [ 1 ].myColumnName ) 
end 

So how would I use this in terms of

setPlayerMoney(something.somthing?)

Thanks also!

Link to comment
If you are using it without a loop, then you need to include the [ 1 ] before the column name from which you wish to obtain it's value, this "1" represents the index.

I'm totally stuck, i'm only trying to get some values from mySQL, maybe i'm going about it wrong? I stored it in mySQL, now i'm trying to take a certain value (money ) out.

Link to comment
You can use WHERE clause to select specific row.

http://www.w3schools.com/sql/sql_where.asp

So how would i setPlayerMoney when retrieving this information?

local playermoney = dbQuery ( server, "SELECT 'money' FROM 'accounts' WHERE serial = '"..serial.."' " ) 

local query = dbQuery ( handler, "SQL STRING HERE" ) 
local data = dbPoll  ( query, - 1 ) -- -1 = wait for the result, freezes server until it has the result. 
if ( type ( data ) == "table" ) then 
    print ( data [ 1 ].myColumnName ) 
end 

Link to comment
You can use WHERE clause to select specific row.

http://www.w3schools.com/sql/sql_where.asp

So how would i setPlayerMoney when retrieving this information?

local playermoney = dbQuery ( server, "SELECT 'money' FROM 'accounts' WHERE serial = '"..serial.."' " ) 

local query = dbQuery ( handler, "SQL STRING HERE" ) 
local data = dbPoll  ( query, - 1 ) -- -1 = wait for the result, freezes server until it has the result. 
if ( type ( data ) == "table" ) then 
    print ( data [ 1 ].myColumnName ) 
end 

I honestly don't understand at all, surely once I retrieved the info from the money index

local playermoney = dbQuery ( server, "SELECT 'money' FROM 'accounts' WHERE serial = '"..serial.."' " ) 

I can simply apply it to setPlayerMoney

Link to comment

That's my code

    function setMoney() 
    serial = getPlayerSerial(source) 
     local data = dbPoll(dbQuery(server, "SELECT 'money' FROM 'accounts' WHERE serial = '"..serial.."' "), -1) 
        setPlayerMoney(player, data[1]["money"]) 
        if data then 
     local curmoney = getPlayerMoney(source) 
     exports.scoreboard:addScoreboardColumn("Money") 
     setElementData(thePlayer,"Money",curmoney,true) 
    end 
 end 
addEventHandler("onPlayerJoin", root, setMoney) 
     

It gives the error; attempt to index local 'data' "a boolean value"

Link to comment
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"]) 
    end 
 end 
addEventHandler("onPlayerJoin", root, setMoney) 

Link to comment
No, When you select the row there might be more than one row so we select the first row in the table which [1].

Oh Thanks, but what happens now is, everytime the player joins, a new row is created instead of checking for previous log ins and updating them! So far is this

checkforserial = dbQuery (server, "ALTER TABLE accounts, ADD CHECK ('"..theserial.."'>0)") 
    if (checkforserial == false) then 
    dbQuery ( server, "INSERT INTO accounts (serial, money) VALUES (?, ?)", tostring (theserial), tostring (themoney) ) 
    else 
  

Link to comment
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" and #data > 0 then 
        setPlayerMoney(source, data[1]["money"]) 
        setElementData(source, "Money", data[1]["money"]) 
    else 
        dbExec(server, "INSERT INTO accounts VALUES (?, ?)", Serial, "0") 
        setElementData(source, "Money", 0) 
    end 
end 
addEventHandler("onPlayerJoin", root, setMoney) 

Use UPDATE if you want change existing value.

http://www.w3schools.com/sql/sql_update.asp

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