So I'm using tmysql to get some data from a mysql database and i want to put that data in a table and then use that data somewhere else in my code.
Getting data and putting it in a table:
[lua]
local StatColumns = {}
table.insert(StatColumns, "test")
tmysql.query("SELECT `COLUMN_NAME` FROM information_schema.`COLUMNS` WHERE `TABLE_SCHEMA`='gmodstats' AND `TABLE_NAME`='player_stats'", function(q)
for x=3, #q do --Start at 3 to skip the first two columns
StatColumns[x] = q[x][1]
end
PrintTable(StatColumns) --This will equal to what i want
end)
PrintTable(StatColumns) --This will equal to only: 1 = test for some reason, this is the problem.
[/lua]
Can anyone help me with this? I can't figure this out.
Doing an sql query takes time. That's why there's a callback function. When the PrintTable on line 10 is run, the sql query hasn't finished yet, and the function on line 5 to 8 hasn't ran yet.
[QUOTE=Divran;36772632]Doing an sql query takes time. That's why there's a callback function. When the PrintTable on line 10 is run, the sql query hasn't finished yet, and the function on line 5 to 8 hasn't ran yet.[/QUOTE]
Makes sense. Is the only way to fix it to add a delay before using the table or using the table only in the callback function ?
Thanks.
[QUOTE=nReCon;36772654]Makes sense. Is the only way to fix it to add a delay before using the table or using the table only in the callback function ?
Thanks.[/QUOTE]
It's safest to use the table in the callback function, since I don't think there's any way of knowing beforehand how long time a query is going to take.
[QUOTE=Divran;36773715]It's safest to use the table in the callback function, since I don't think there's any way of knowing beforehand how long time a query is going to take.[/QUOTE]
exactly. You dont.
Sorry, you need to Log In to post a reply to this thread.