PHP code example of twinkle / database

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

    

twinkle / database example snippets


$db = new \Twinkle\Database\DB([
    "dsn" => "mysql:host=127.0.0.1;port=3306;dbname=db_demo",
    "username" => 'test',
    "password" => 'test_pass'
]);
$info = $db->execQuery(
    (new \Twinkle\Database\Query())->select('user_id,email')
        ->from('user_info')
        ->where('user_id = ?',1992)
        ->limit(1)
)->fetchInto();

$master = function () {
    return new \Twinkle\Database\DB([
        "dsn" => "mysql:host=master.test.db;port=3306;dbname=db_demo",
        "username" => 'master_user',
        "password" => 'master_pass'
    ]);
};

$slave1 = function () {
    return new \Twinkle\Database\DB([
        "dsn" => "mysql:host=slave1.test.db;port=3306;dbname=db_demo",
        "username" => 'slave1_test',
        "password" => 'slave1_pass'
    ]);
};

$slave2 = function () {
    return new \Twinkle\Database\DB([
        "dsn" => "mysql:host=slave2.test.db;port=3306;dbname=db_demo",
        "username" => 'slave2_test',
        "password" => 'slave2_pass'
    ]);
};

$connection = new \Twinkle\Database\Connection();
$connection->setWrite($master);
$connection->setRead('slave1',$slave1);
$connection->setRead('slave2',$slave2);

$info = $connection->getRead()->execQuery(
    (new \Twinkle\Database\Query())->select('user_id,email')
        ->from('user_info')
        ->where('user_id = ?',1992)
        ->limit(1)
)->fetchInto();