Skip to content

Parameter binding

Greg Bowler edited this page Mar 3, 2026 · 5 revisions

Here, we will look at all binding styles supported by PHP.Gt/Database.

Positional placeholders (?)

select * from user where id = ?
$user = $db->fetch("user/getById", 42);

Named placeholders (:name)

select * from user where email = :email and isActive = :isActive
$user = $db->fetch("user/getByEmail", [
	"email" => "dev@example.com",
	"isActive" => true,
]);

Multiple array inputs

Bindings are flattened, so the following are equivalent:

$db->fetch("user/getByEmail", ["email" => "a@example.com", "isActive" => 1]);
$db->fetch("user/getByEmail", ["email" => "a@example.com"], ["isActive" => 1]);
$db->fetch("user/getByEmail", [["email" => "a@example.com"], ["isActive" => 1]]);

Value conversion rules

  • bool becomes 0 or 1
  • DateTimeInterface becomes Y-m-d H:i:s
  • arrays are expanded to indexed placeholders such as :ids__0, :ids__1

Special bindings

These values are injected into SQL after sanitisation:

  • :groupBy
  • :orderBy
  • :limit
  • :offset
  • :infileName

Example:

select id, email from user
order by :orderBy
limit :limit offset :offset
$rows = $db->fetchAll("user/list", [
	"orderBy" => "id desc",
	"limit" => 20,
	"offset" => 40,
]);

Dynamic bindings

  • :__dynamicValueSet for generated multi-row insert value sets
  • :__dynamicIn for runtime IN (...) content
  • :__dynamicOr for grouped OR conditions

Now we can build flexible query templates without manual string concatenation in application logic.

Example code: example/04-dynamic-bindings.php

Clone this wiki locally