ServersYour serverFor creatorsForumFAQDocumentationSecurity
Download the launcher
  1. Documentation
  2. Server functions
  3. dbQuery

Function

dbQuery

ServerDatabaseIdentical to MTA:SA

Sends a query that returns a result. Collect it with dbPoll or in a callback that receives the query handle.

Syntax

handle dbQuery([function callback, table callbackArgs,] element connection, string query [, mixed ...])

Arguments

callbackfunction, optional = nil
Called when the result is ready. Its first argument is the query handle.
callbackArgstable, optional = nil
Extra arguments for the callback.
connectionelement
A handle from dbConnect.
querystring
SQL query with ? placeholders.
...mixed
Values for the ? placeholders.

Returns

A query handle or false.

Example

addEventHandler("onPlayerLogin", root, function()
    local player = source
    dbQuery(function(q)
        local rows = dbPoll(q, 0)
        if rows and rows[1] and isElement(player) then
            setPlayerMoney(player, rows[1].kasa)
        end
    end, db, "SELECT kasa FROM gracze WHERE serial = ?", getPlayerSerial(player))
end)

Notes

Does not block the server. The query runs in the background and the callback fires when the database answers, always after dbQuery has returned. Queries on one connection run in order.

Simpler: dbFetch. If you do not need a callback, dbFetch inside an async task gives you the rows in one call.

See also