Skip to content
Greg Bowler edited this page Apr 2, 2026 · 10 revisions

This library gives us a tidy way to keep SQL close to the application, without mixing query strings throughout our PHP code.

We can organise queries into collections, bind values safely, fetch predictable PHP types, and keep schema changes under version control.

Note

If you are building with WebEngine, most of the setup is already done for you. The framework wires up the database service, reads configuration, and exposes the migration command through gt migrate. The WebEngine overview page is at https://www.php.gt/docs/webengine/database/.

What this library does

  • Stores queries in files or PHP query classes.
  • Executes named queries through one Database object.
  • Supports positional and named parameter binding.
  • Returns rows and result sets through consistent helper types.
  • Supports multiple named connections.
  • Runs schema migrations with integrity checks.
  • Supports branch-local development migrations as well as canonical migrations.

Recommended reading order

  1. Quick start guide
  2. Configuration and connections
  3. Query collections
  4. Parameter binding
  5. Raw SQL and result sets
  6. Type-safe getters
  7. Database migrations
  8. Examples

A tiny example

use Gt\Database\Connection\Settings;
use Gt\Database\Database;

$settings = new Settings(
	"query",
	Settings::DRIVER_SQLITE,
	"app.sqlite"
);

$db = new Database($settings);
$userRow = $db->fetch("user/getById", 42);
echo "User email address: " . $userRow->getString("email");

In a nutshell: queries live on disk, the PHP code calls them by name, and the result comes back through a small type-safe API.


To see the full setup from scratch, move on to the Quick start guide.

Clone this wiki locally