Database
db_query:fetch
Server
Retrieves all rows matching the prepared query from the table
Syntax
local rows = db_query:fetch(limit = 0)Parameters
| Type | Name | Description |
|---|---|---|
int | limit | Limits the maximum number of rows to retrieve When set to 0, no upper limit is applied and all matching rows are returned |
Returns
| Type | Name | Description |
|---|---|---|
table | rows | Array of matching rows, where each row is a table of column name to value pairs |
Examples
local db = database.create("127.0.0.1", "root", "", "vital_sandbox")
--Fetch all players with a score greater than 100
local rows = db:table("players")
:select("id", "name", "score")
:where("score", ">", 100)
:fetch()
--Fetch only the top 5 players with a score greater than 100
local top5 = db:table("players")
:select("id", "name", "score")
:where("score", ">", 100)
:fetch(5)
--Iterate over the results and print each row
for i = 1, #rows do
engine.print("info", rows[i].id, rows[i].name, rows[i].score)
end