Skip to content

Quick start guide

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

Here, we will build a minimal working setup in a bare repository, and run our first query.

Note

WebEngine projects already have the Database object instantiated, available to your code's go function by the ServiceLoader.

1. Install the package

composer require phpgt/database

2. Create your first query file

Create the file query/user/getById.sql:

select id, email
from user
where id = ?
limit 1

3. Configure the database client

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

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

$db = new Database($settings);

4. Execute the query

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

echo $user?->email;

5. Add basic CRUD operations

$newId = $db->insert("user/insert", ["email" => "dev@example.com"]);
$rowsUpdated = $db->update("user/updateEmail", ["id" => $newId, "email" => "new@example.com"]);
$rowsDeleted = $db->delete("user/delete", ["id" => $newId]);

Now we can move on to parameter binding and typed results.


If you'd like to learn more of the basics first, move on to Configuration and connections or Query collections.

Clone this wiki locally