Maurize Posted January 24, 2016 Posted January 24, 2016 (edited) Hello Ladies and Gentleman, I'm tying to create a function which allows me to get fields from my mysql table. What I've done so far: local db = dbConnect("mysql", "host=localhost;dbname=mtasa", "root", ""); function getData(player, db_table, db_row) dbQuery(function(e) local result = dbPoll(e, 0); return result[db_row]; end, db, "SELECT "..db_row.." FROM "..db_table.." WHERE username = '"..getPlayerName(player).."' LIMIT 1"); end This gives an error if I try to outputChatBox the result. Maybe someone can help me up with this. Edited January 24, 2016 by Guest
Castillo Posted January 24, 2016 Posted January 24, 2016 You shouldn't be connecting to MySQL everytime you want to retrive data, make a single connection and then use the connection handler for the rest of the functions.
Maurize Posted January 24, 2016 Author Posted January 24, 2016 Hey, Solidsnake14. Thanks, this is a useful hint (performance...). Anyway something seems like to fail. Sadly there is no way to print the whole table.
Castillo Posted January 24, 2016 Posted January 24, 2016 This will get the first result in the table: result [ 1 ] [ db_row ]
Maurize Posted January 24, 2016 Author Posted January 24, 2016 Yeah I thought the same but this didn't solved my problem. If I try to outputChatBox the result it gives an error -> Expected string Current function: local db = dbConnect("mysql", "host=localhost;dbname=mtasa", "root", ""); function getData(player, db_table, db_row) dbQuery(function(e) local r = dbPoll(e, 0); return r[1][db_row]; end, db, "SELECT "..db_row.." FROM "..db_table.." WHERE username = '"..getPlayerName(player).."' LIMIT 1"); end I think the async makes problems. If I do the query directly in database it works like charm but not in .lua.
Castillo Posted January 24, 2016 Posted January 24, 2016 The problem is that you are trying to make getData return a value it doesn't have, the callback function has the value.
Maurize Posted January 24, 2016 Author Posted January 24, 2016 Right. This makes sense, Castillo. This means an if-statement where I call data from database is impossible?
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