Your first complete application using PDOdb.
<?php
require 'vendor/autoload.php';
use tommyknocker\pdodb\PdoDb;
use tommyknocker\pdodb\helpers\Db;
// Connect to SQLite (no setup required)
$db = new PdoDb('sqlite', [
'path' => ':memory:'
]);
// Create tables
$db->rawQuery('
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT, -- INT AUTO_INCREMENT in MySQL, SERIAL in PostgreSQL
name TEXT NOT NULL, -- VARCHAR(255) in MySQL/PostgreSQL
email TEXT NOT NULL, -- VARCHAR(255) in MySQL/PostgreSQL
age INTEGER, -- INT in MySQL/PostgreSQL
created_at DATETIME DEFAULT CURRENT_TIMESTAMP -- TIMESTAMP in MySQL/PostgreSQL
)
');
$db->rawQuery('
CREATE TABLE posts (
id INTEGER PRIMARY KEY AUTOINCREMENT, -- INT AUTO_INCREMENT in MySQL, SERIAL in PostgreSQL
user_id INTEGER NOT NULL,
title TEXT NOT NULL,
content TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id)
)
');
echo "=== INSERT ===\n";
// Insert single user
$userId = $db->find()->table('users')->insert([
'name' => 'Alice',
'email' => 'alice@example.com',
'age' => 30
]);
echo "Inserted user with ID: $userId\n";
// Insert multiple users
$users = [
['name' => 'Bob', 'email' => 'bob@example.com', 'age' => 25],
['name' => 'Carol', 'email' => 'carol@example.com', 'age' => 28]
];
$count = $db->find()->table('users')->insertMulti($users);
echo "Inserted $count users\n";
// Insert with current timestamp
$db->find()->table('posts')->insert([
'user_id' => $userId,
'title' => 'My First Post',
'content' => 'This is my first blog post!',
'created_at' => Db::now()
]);
echo "\n=== SELECT ===\n";
// Get all users
$allUsers = $db->find()->from('users')->get();
foreach ($allUsers as $user) {
echo "User: {$user['name']} ({$user['email']}) - Age: {$user['age']}\n";
}
// Get single user
$user = $db->find()
->from('users')
->where('id', $userId)
->getOne();
echo "Found user: {$user['name']}\n";
// Get users with conditions
$adults = $db->find()
->from('users')
->where('age', 18, '>=')
->orderBy('age', 'DESC')
->limit(5)
->get();
echo "\nAdults (age >= 18):\n";
foreach ($adults as $user) {
echo "- {$user['name']} (Age: {$user['age']})\n";
}
echo "\n=== UPDATE ===\n";
// Update single user
$affected = $db->find()
->table('users')
->where('id', 1)
->update([
'age' => 31,
'email' => 'alice.new@example.com'
]);
echo "Updated $affected user(s)\n";
// Increment age
$db->find()
->table('users')
->where('id', 1)
->update([
'age' => Db::inc()
]);
echo "Incremented age\n";
echo "\n=== DELETE ===\n";
// Delete users
$deleted = $db->find()
->table('users')
->where('id', 3)
->delete();
echo "Deleted $deleted user(s)\n";
echo "\n=== JOIN ===\n";
// Join users and posts
$results = $db->find()
->from('users AS u')
->select([
'u.id',
'u.name',
'post_count' => 'COUNT(p.id)'
])
->leftJoin('posts AS p', 'p.user_id = u.id')
->groupBy('u.id', 'u.name')
->get();
foreach ($results as $result) {
echo "User: {$result['name']} has {$result['post_count']} posts\n";
}
echo "\n=== FINAL STATE ===\n";
$finalUsers = $db->find()->from('users')->get();
echo "Total users: " . count($finalUsers) . "\n";
foreach ($finalUsers as $user) {
echo "- {$user['name']} (ID: {$user['id']})\n";
}php hello-world.phpPDODB_DRIVER=mysql php hello-world.phpPDODB_DRIVER=pgsql php hello-world.php- Connection - Connect to a database
- Database Structure - Create tables (DDL operations)
- Insert - Single and multiple inserts
- Select - Basic SELECT queries
- Where - Filtering with conditions
- Update - Updating records
- Delete - Deleting records
- Join - Joining tables
- Aggregations - Using COUNT with GROUP BY
The query builder uses a fluent, chainable API:
$db->find()
->from('users')
->where('age', 18, '>=')
->orderBy('name', 'ASC')
->limit(10)
->get();Methods can be chained together:
$db->find()
->from('users')
->where('status', 'active')
->andWhere('age', 18, '>')
->orderBy('created_at', 'DESC')
->limit(20);All queries automatically use prepared statements for security:
// Safe from SQL injection
$users = $db->find()
->from('users')
->where('id', $userId) // Automatically parameterized
->get();Use helper functions for common operations:
use tommyknocker\pdodb\helpers\Db;
$db->find()->table('users')->update([
'age' => Db::inc(), // Increment by 1
'updated_at' => Db::now() // Current timestamp
]);- Query Builder Basics - Deep dive into the query builder
- SELECT Operations - Learn SELECT queries
- Data Manipulation - INSERT, UPDATE, DELETE