PHP code example of decmuc / pdodb

1. Go to this page and download the library: Download decmuc/pdodb library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

decmuc / pdodb example snippets


$user = $db->whereInt('id', 42)->getOne('users');
// → SELECT * FROM users WHERE id = 42 LIMIT 1


use decMuc\PDOdb\PDOdb;

$db = new PDOdb([
    'host'     => 'localhost',
    'username' => 'root',
    'password' => 'secret',
    'db'       => 'my_database',
    'charset'  => 'utf8mb4',
]);

// Step by step – easy to read and extend
$db->whereInt('active', 1);
$db->whereString('role', 'admin');
$db->orderBy('created_at', 'DESC');
$users = $db->get('users');

// Fluent – compact for experienced devs
$users = $db->whereInt('active', 1)
            ->whereString('role', 'admin')
            ->orderBy('created_at', 'DESC')
            ->get('users');

// → SELECT * FROM users WHERE active = 1 AND role = 'admin' ORDER BY created_at DESC

// Boolean flags – the clean way
$db->whereBool('is_active', true)->get('users');
// → SELECT * FROM users WHERE is_active = 1

// Date range
$db->whereDateBetween('created_at', '2025-01-01', '2025-12-31')->get('orders');
// → SELECT * FROM orders WHERE created_at BETWEEN '2025-01-01' AND '2025-12-31'

// LIKE with wildcard modes
$db->whereLike('name', 'john', 'both')->get('users');
// → SELECT * FROM users WHERE name LIKE '%john%'

// WHERE groups with parentheses
$db->whereInt('active', 1)
   ->whereGroup(function($q) {
       $q->whereString('role', 'admin')
         ->orWhereString('role', 'editor');
   })
   ->get('users');
// → SELECT * FROM users WHERE active = 1 AND (role = 'admin' OR role = 'editor')

// Raw query with named placeholders
$db->rawQuery("SELECT * FROM users WHERE id = :id AND role = :role", [
    'id'   => 42,
    'role' => 'admin',
]);

// Insert & get ID
$id = $db->insert('users', [
    'name'  => 'Alice',
    'email' => '[email protected]',
    'active' => 1,
]);

// Transactions
$db->startTransaction();
try {
    $db->insert('orders', ['user_id' => 1, 'total' => 49.99]);
    $db->update('users', ['last_order' => date('Y-m-d')], $db->whereInt('id', 1));
    $db->commit();
} catch (\Throwable $e) {
    $db->rollback();
}

try {
    $db->whereInt('id', 'not-a-number');
    $user = $db->getOne('users');
} catch (\InvalidArgumentException $e) {
    echo $e->getMessage();
    // → Invalid integer value for column 'id'.
}