# dbQuery (Server)

> Sends an SQL query whose answer is read later with `dbPoll`.

- **Platform**: MY Multiplayer (MYMP)
- **Kind**: Lua Function
- **Side**: Server
- **Category**: Database
- **URL**: https://wiki.mymp.gg/en/dbQuery

## Syntax

```lua
any dbQuery(element connection, string sql, any ... [, function callback = nil])
```

## Parameters

| Name | Type | Description |
| --- | --- | --- |
| `callback` | `function` | If given, it is called with the query handle when the answer arrives; call `dbPoll(query, 0)` inside it. *(default: `nil`)* |
| `connection` | `element` | A connection from `dbConnect`. |
| `sql` | `string` | The SQL statement, with `?` and `??` as placeholders. |
| `...` | `any` | The placeholders' values: strings, numbers, booleans or `nil`. |

## Returns

A query handle for `dbPoll` and `dbFree`. Without `callback`, inside a coroutine, it waits for the answer and returns the same as `dbPoll`.

### Placeholders and handle lifetime

- `?` takes the next value, escaped: `nil` becomes `NULL`, booleans become `1` or `0` and strings are quoted.
- `??` takes a string as an escaped table or column identifier.
- The number of values must match the number of placeholders.
- A handle without `callback` that nothing references any more is freed automatically by the garbage collector.

## Example

```lua
dbQuery(function(query)
    local rows = dbPoll(query, 0)
    if rows then
        print(#rows .. " players saved")
    end
end, db, "SELECT * FROM players WHERE money > ?", 100)
```

## See also

- [dbPoll (Server)](https://wiki.mymp.gg/en/dbPoll.md): Reads the answer of a dbQuery query, waiting for it if needed.
- [dbFree (Server)](https://wiki.mymp.gg/en/dbFree.md): Discards a dbQuery query whose answer will not be read.
- [dbExec (Server)](https://wiki.mymp.gg/en/dbExec.md): Runs an SQL statement without waiting for an answer.
