Ziemo Posted August 7, 2019 Share Posted August 7, 2019 Hi, I've started writing scripts connected to the database, and I struggle why the current script that I am working on doesn't work. Info: - Name of the table: accounts - Columns: username, skin, money - No errors Here is my script: function showStats(player) local playerName = getPlayerName(player) local qh = dbQuery(db, "SELECT skin, money FROM accounts WHERE username=?", playerName) local result = dbPoll(qh, -1) if result then for _, row in ipairs(result) do outputChatBox("Skin: " ..row["skin"].. "Money: "..row["money"] .. "") end end end addCommandHandler("stats", showStats) Link to comment
DNL291 Posted August 7, 2019 Share Posted August 7, 2019 Are you sure username is the player name and not the account name? @Ziemo Link to comment
Ziemo Posted August 7, 2019 Author Share Posted August 7, 2019 @DNL291 I have set player's name in mta to the same as in database Link to comment
DNL291 Posted August 7, 2019 Share Posted August 7, 2019 Do a test with this code: function showStats(player) local playerName = getPlayerName(player) local qh = dbQuery(db, "SELECT skin, money FROM accounts WHERE username=?", playerName) local result = dbPoll(qh, -1) if result then for _, row in ipairs(result) do outputChatBox("Skin: " ..row["skin"].. "Money: "..row["money"] .. "") end end iprint( "qh " .. qh ..", result: ".. result ) end addCommandHandler("stats", showStats) Also, post the code where you create the table and save these values if possible. 1 Link to comment
Ziemo Posted August 7, 2019 Author Share Posted August 7, 2019 It works, It outputted valid values, but I see an error: attempt to concatenate local 'result' (a table value) Link to comment
DNL291 Posted August 7, 2019 Share Posted August 7, 2019 Try: function showStats(player) local playerName = getPlayerName(player) local qh = dbQuery(db, "SELECT skin, money FROM accounts WHERE username=?", playerName) local result = dbPoll(qh, -1) if result then result = result[1] for _, row in ipairs(result) do outputChatBox("Skin: " ..row["skin"].. "Money: "..row["money"] .. "") end end end addCommandHandler("stats", showStats) @Ziemo 1 Link to comment
Ziemo Posted August 7, 2019 Author Share Posted August 7, 2019 (edited) This new function doesn't work, but when I delete: iprint( "qh " .. qh ..", result: ".. result ) from the older code, error doesn't show up EDIT: never mind it showed, I just missed it Edited August 7, 2019 by Ziemo Link to comment
Moderators IIYAMA Posted August 7, 2019 Moderators Share Posted August 7, 2019 Concat with iprint like this: iprint( "qh ", qh, ", result: ", result ) @Ziemo 1 Link to comment
Ziemo Posted August 7, 2019 Author Share Posted August 7, 2019 It works, shows the info in debug, and outputs skin and money from database! Thanks you both guys. 1 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