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->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,
]);
$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]);