PHP code example of felixkiss / database

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

    

felixkiss / database example snippets




use Felixkiss\Database\Database;

$pdo = new PDO('mysql:dbname=foo;host=127.0.0.1', 'foo', 'bar');
$db = new Database($pdo);

$db->execute('TRUNCATE some_table');

$user = $db->select('SELECT * FROM user');
foreach ($users as $user)
{
    // Do something ...
}

$users = $db->select('SELECT * FROM user WHERE age BETWEEN ? AND ?', [20, 40]);

$users = $db->select(
  'SELECT * FROM user WHERE age BETWEEN :young AND :old LIMIT 0, :limit', [
  ':young' => 20,
  ':old'   => 40,
  ':limit' => 10,
]);

$users = $db->lists('SELECT username FROM users');

$count = $db->pluck('SELECT COUNT(*) FROM users');

$db->insert('users', [
    'username' => 'felixkiss',
    'location' => 'Vienna, Austria',
]);

$db->update('users', [
  'location' => 'Toronto, Canada',
  ], 'WHERE username = ?', ["felixkiss"]
);

$read = new PDO('mysql:dbname=foo;host=127.0.0.1', 'foo', 'bar');
$write = new PDO('mysql:dbname=foo;host=mirror.example.com', 'foo', 'bar');
$db = new Database($read, $write);

$db->execute('TRUNCATE users'); // runs on write connection
$db->execute('LOCK TABLE users WRITE', [], true); // runs on read connection