PHP code example of roolith / database

1. Go to this page and download the library: Download roolith/database 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/ */

    

roolith / database example snippets


use Roolith\Store\Database;

$db = new Database();
$db->connect([
    'host' => 'host',
    'name' => 'dbname',
    'user' => 'username',
    'pass' => 'password',
]);

// Get all users
$users = $db->query("SELECT * FROM users")->get();
print_r($users);

// Get all usernames
$usernames = $db->table('users')->select([
    'field' => 'name',
])->get();
print_r($usernames);

// Disconnect
$db->disconnect();

$db->query("SELECT * FROM users")->get();

$db->table('users')->select([
    'field' => ['name', 'email'],
    'condition' => 'WHERE id > :min',
    'bindings' => [':min' => 0],
    'limit' => '0, 10',
    'orderBy' => 'name',
    'groupBy' => 'name',
])->get();

$result = $db->table('users')->insert(
    ['name' => 'Brannon Bruen', 'email' => '[email protected]']
);

print_r($result->success());

$result = $db->table('users')->insert(
    ['name' => 'John doe', 'email' => '[email protected]'],
    ['email']
);

$result->affectedRow();
$result->insertedId();
$result->isDuplicate();
$result->success();

$result = $db->table('users')->update(
    ['name' => 'Habib Hadi', 'email' => '[email protected]'],
    ['id' => 1]
);

$result = $db->table('users')->update(
    ['username' => 'johndoe'],
    ['id' => 4],
    ['username']
);

$result->affectedRow();
$result->isDuplicate();
$result->success();

$result = $db->table('users')->delete(['id' => 4]);

$result->affectedRow();
$result->success();

$db = new Database();
$db->connect([
    'host' => 'host',
    'name' => 'dbname',
    'user' => 'username',
    'pass' => 'password',
]);

$db = new Database([
   'host' => 'host',
   'name' => 'dbname',
   'user' => 'username',
   'pass' => 'password',
]);

$db->disconnect();

$db->table('users')->where('name', '%Hadi%', 'LIKE')->get();
// new bound style also works
$db->table('users')->where('age', '>', 18)->get();
$db->table('users')->orderBy('id', 'DESC')->limit(10)->offset(5)->get();

$db->table('users')->find(1);

$db->table('users')->pluck(['name', 'email']);

$db->query("SELECT id FROM users")->count();

$total = $db->query("SELECT id FROM users")->count();
$result = $db->query("SELECT * FROM users")->paginate([
    'perPage' => 5,
    'pageUrl' => 'http://domain.com',
    'primaryColumn' => 'id',
    'pageParam' => 'page',
    'total' => $total,
]);

$total = $db->query("SELECT id FROM users")->count();
$result = $db->query("SELECT * FROM users")->paginate([
    'perPage' => 5, // default 20
    'total' => $total,
]);

use Roolith\Store\Paginate;

$paginate = Paginate::fromRequest(
    ['perPage' => 5, 'total' => $total],
    ['REQUEST_URI' => '/users'],
    ['page' => 2],
);

$db->transaction(function ($db) {
    $db->table('users')->insert(['name' => 'A', 'email' => '[email protected]']);
    $db->table('orders')->insert(['user_email' => '[email protected]', 'total' => 100]);
});

$userId = $db->transaction(function ($db) {
    $result = $db->table('users')->insert(['name' => 'C', 'email' => '[email protected]']);
    return $result->insertedId();
});

try {
    $db->transaction(function ($db) {
        $db->table('users')->insert(['name' => 'B', 'email' => '[email protected]']);
        throw new RuntimeException('force rollback');
    });
} catch (RuntimeException $e) {
    // row B was not saved
}

$db->beginTransaction();
try {
    $db->table('users')->insert(['name' => 'D', 'email' => '[email protected]']);
    $db->table('users')->update(['name' => 'D2'], ['email' => '[email protected]']);
    $db->commit();
} catch (Throwable $e) {
    $db->rollBack();
    throw $e;
}

function createUser($db, array $data): void
{
    $run = function () use ($db, $data) {
        $db->table('users')->insert($data);
    };

    if ($db->inTransaction()) {
        $run();
        return;
    }

    $db->transaction($run);
}

$db->transaction(function ($db) {
    createUser($db, ['name' => 'E', 'email' => '[email protected]']);
    createUser($db, ['name' => 'F', 'email' => '[email protected]']);
});

$db->commit(); // throws when no transaction is active
$db->rollBack(); // throws when no transaction is active

$db->beginTransaction();
$db->beginTransaction(); // throws, nesting is unsupported

$db->transaction(function ($db) {
    $db->transaction(function ($db) {}); // throws, nesting is unsupported
});

$db->query("SELECT * FROM users WHERE email = :email", null, [':email' => $email])->get();
$db->execute("DELETE FROM users WHERE id = :id", [':id' => $id]);

print_r($result->getDetails());

$db->debugMode()->table('users')->find(1);
print_r($db->getDebugLog());