MySQL in return functions.

I’m currently working on a project which I would like to integrate MySQL in.

The problem I have is that I’m using functions to get the information I need (Not using this but… GetTime() for example…) and when using MySQL I can’t get them to return something without using query:wait() because I have to wait for the answer to come back from the MySQL server so I have something to return. The problem with query:wait() is that it says “This should only ever be used if it is really necessary, since it can cause lag”.

So… What should I do? Will the server crash if nothing comes back from the MySQL server or should I be fine with query:wait()?

I’m currently using MySQLoo v9.5

Well, query:wait() freezes the entire server until you get a response. Which you SHOULDN’T do.
You can’t “return” a value synchronously.

You’ll have to either:

  1. Use callback functions, somewhat similar to what the sound.PlayURL function does in GMod
  2. Figure out coroutines to make “asynchronous functions”

1 is the easiest solution
2 is the cleanest solution

To counter Lego, unless I understood him incorrectly, you can return a value synchronously through the power of scope:

[lua]function DoThing()
local ret

local query = db:query("SELECT stuff FROM things")
query.onSuccess = function(_, res)
    ret = res

return ret


However using wait() is a really bad practice, as you’ve noted.

Sometimes what you want to do can be easily accomplished by allowing the server to “predict” what would be in the database without needing to call to it. For example, getting a player’s total playtime can be accomplished by storing the player’s total play time and connection time when they join, and then getting the value is as simple as playtimeAtConnection + (CurTime() - connectionTime)

You shouldn’t attempt to* return a value synchronously :v: