- Documentation
- Server functions
- dbFetchValue
Function
dbFetchValue
Runs a query and returns a single value: the first column of the first row. For counting (COUNT(*)), sums and single fields.
Syntax
mixed dbFetchValue(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 value from the first column of the first row, nil when there are no rows. On error false with the error message as the second value.
Example
local db = dbConnect("sqlite", "gracze.db")
addCommandHandler("ilu", function(player)
async(function()
local ilu = dbFetchValue(db, "SELECT COUNT(*) FROM gracze")
outputChatBox("Zarejestrowanych graczy: " .. tostring(ilu), player)
end)
end)
Notes
false can mean NULL or an error. A NULL value from the database also arrives as false. If the column can be empty, check the second value: on error it holds the message.
See also
dbFetchRuns a query and returns its result right away. One call instead ofdbQuery,dbPollanddbFree. Works with SELECT, INSERT, UPDATE and DELETE.dbFetchOneRuns a query and returns only the first row. Handy when you look for a single record, for example a player's account.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.