- Documentation
- Server functions
- dbFetchOne
Function
dbFetchOne
Runs a query and returns only the first row. Handy when you look for a single record, for example a player's account.
Syntax
table dbFetchOne(element connection, string query, mixed ...)Arguments
connectionelement- A handle from
dbConnect. querystring- SQL query with
?in place of values. ...mixed- Values for the
?placeholders.
Returns
The first row (column = value), nil if the query returned no rows, or false and the error message.
Example
local db = dbConnect("mysql", "glowna")
addEventHandler("onPlayerJoin", root, function()
local player = source
async(function()
local konto = dbFetchOne(db, "SELECT kasa, skin FROM gracze WHERE serial = ?", getPlayerSerial(player))
if not isElement(player) then return end
if konto then
setPlayerMoney(player, konto.kasa)
setElementModel(player, konto.skin)
else
outputChatBox("Witaj pierwszy raz na serwerze!", player)
end
end)
end)
See also
dbFetchRuns a query and returns its result right away. One call instead ofdbQuery,dbPollanddbFree. Works with SELECT, INSERT, UPDATE and DELETE.dbFetchValueRuns a query and returns a single value: the first column of the first row. For counting (COUNT(*)), sums and single fields.asyncRuns a function as a task (coroutine). The code in the task runs immediately until its first wait (sleep,await,waitForEvent). Then it yields and the server and the game keep running. When the wait is over, the task continues from where it stopped. This lets you write steps spread over time (a countdown, a database query, an HTTP response, waiting for a player) one after another, without nested callbacks and timers.