Skip to content
Greg Bowler edited this page Aug 31, 2026 · 11 revisions

phpgt/database gives us a structured way to keep database access close to the application without scattering SQL strings throughout our PHP code.

We can keep queries in files or PHP classes, bind values safely, work with predictable result objects, and apply schema changes in a controlled order.

Note

If we are building with WebEngine, most of the wiring is already in place. WebEngine reads the database configuration, exposes the database service in page logic, and makes the migration command available through gt migrate. The framework overview is at https://www.php.gt/docs/webengine/database/.

What this library covers

  • Query files organised into collections.
  • PHP query collections, including phpgt/sqlbuilder builders.
  • Positional, named, special, and dynamic parameter binding.
  • Typed fetch helpers for common single-column queries.
  • Raw SQL execution when a named query file would not add much.
  • Multiple named connections in one Database object.
  • Schema migrations with sequence and integrity checks.

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. Multiple connections
  8. Database migrations
  9. Examples

A small example

use GT\Database\Connection\Settings;
use GT\Database\Database;

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

$db = new Database($settings);
$user = $db->fetch("user/getById", 42);

echo $user?->getString("email");

In practice, that means the PHP code names the query, the SQL stays on disk, and the returned row is already wrapped in the helper API used throughout the rest of the library.


To see the setup from scratch, continue with the Quick start guide.

Clone this wiki locally