Lloyd Logan Posted January 11, 2014 Share Posted January 11, 2014 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
Castillo Posted January 11, 2014 Share Posted January 11, 2014 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
Lloyd Logan Posted January 11, 2014 Author Share Posted January 11, 2014 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
Castillo Posted January 11, 2014 Share Posted January 11, 2014 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. Link to comment
Lloyd Logan Posted January 11, 2014 Author Share Posted January 11, 2014 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
TAPL Posted January 11, 2014 Share Posted January 11, 2014 Have you already got the MySQL connected? dbConnect If you want to get certain value you need to use SELECT. http://www.w3schools.com/sql/sql_select.asp Link to comment
Lloyd Logan Posted January 11, 2014 Author Share Posted January 11, 2014 Have you already got the MySQL connected? dbConnect If you want to get certain value you need to use SELECT. http://www.w3schools.com/sql/sql_select.asp I used SELECT, but how would i select something from a specific row? Link to comment
TAPL Posted January 11, 2014 Share Posted January 11, 2014 I don't understand, can you post your code? Link to comment
Lloyd Logan Posted January 12, 2014 Author Share Posted January 12, 2014 I don't understand, can you post your code? I'll try again, if not i'll post my code I have multiple rows in mySQL such as money position etc But how do I select money from a specific say, account name or serial? Link to comment
Lloyd Logan Posted January 12, 2014 Author Share Posted January 12, 2014 I don't understand, can you post your code? I'll try again, if not i'll post my code I have multiple rows in mySQL such as money position etc But how do I select money from a specific say, account name or serial? Link to comment
TAPL Posted January 12, 2014 Share Posted January 12, 2014 You can use WHERE clause to select specific row. http://www.w3schools.com/sql/sql_where.asp Link to comment
Lloyd Logan Posted January 12, 2014 Author Share Posted January 12, 2014 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.."' " ) Link to comment
TAPL Posted January 12, 2014 Share Posted January 12, 2014 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
Lloyd Logan Posted January 12, 2014 Author Share Posted January 12, 2014 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
TAPL Posted January 12, 2014 Share Posted January 12, 2014 local data = dbPoll(dbQuery(server, "SELECT 'money' FROM 'accounts' WHERE serial = '"..serial.."' "), -1) setPlayerMoney(player, data[1]["money"]) Link to comment
Lloyd Logan Posted January 12, 2014 Author Share Posted January 12, 2014 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
TAPL Posted January 12, 2014 Share Posted January 12, 2014 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
Lloyd Logan Posted January 12, 2014 Author Share Posted January 12, 2014 Thank you!! One more question, does the data[1]["money"]) [1] just mean to unfreeze the dbPoll? Link to comment
TAPL Posted January 12, 2014 Share Posted January 12, 2014 No, When you select the row there might be more than one row so we select the first row in the table which [1]. Link to comment
Lloyd Logan Posted January 12, 2014 Author Share Posted January 12, 2014 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
TAPL Posted January 12, 2014 Share Posted January 12, 2014 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now