PHP code example of knj / d2o

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

    

knj / d2o example snippets


$d2o = new Wazly\D2O($dsh, $username, $password);

$sql = 'SELECT * FROM users WHERE id = :id'

$row = $d2o
    ->state($sql)
    ->run([':id' => 3])
    ->pick();

$dbh = new PDO($dsh, $username, $password);

$sql = 'SELECT * FROM users WHERE id = :id';

$stmt = $dbh->prepare($sql);
$stmt->execute([':id' => 3]);
$row = $stmt->fetch(PDO::FETCH_OBJ);

$d2o->state($sql); // returns $d2o

$d2o->state($sql)
    ->bind([
        ':role' => $role,
        ':limit' => [$limit, 'int'],
    ]); // returns $d2o

$d2o->state($sql)
    ->bind([
        ':role' => $role,
        ':limit' => [$limit, 'int'],
    ])
    ->run(); // returns $d2o

$d2o->state($sql)
    ->run([
        ':role' => $role,
        ':limit' => [$limit, 'int'],
    ]); // the same as the above

$row = $d2o->state($sql)
    ->run([
        ':role' => $role,
        ':limit' => [1, 'int'],
    ])
    ->pick(); // recommended if the number of rows is supposed to be 1

$result = $d2o->state($sql)
    ->run([
        ':role' => $role,
        ':limit' => [$limit, 'int'],
    ]); // recommended if the number of rows is supposed to be 2 or more

$row1 = $result->pick();
$row2 = $result->pick();
$row3 = $result->pick();

$rows = $d2o->state($sql)
    ->run([
        ':role' => $role,
        ':limit' => [$limit, 'int'],
    ])
    ->format();

$rows = $d2o->state($sql)->run()->getStatement()->fetchAll();

$d2o->state('INSERT INTO items(name, price) VALUES (:name, :price)')
    ->run(['name' => 'pencil', 'price' => 20])
    ->run(['name' => 'eraser', 'price' => 60])
    ->run(['name' => 'notebook', 'price' => 100]);

$d2o->bind([
    'role' => 'editor',  // ':role' => 'editor', PDO::PARAM_STR
    'limit' => 20,       // ':limit' => 20, PDO::PARAM_INT
    'hash' => '6293',    // ':hash' => '6293', PDO::PARAM_STR
    'id' => [36, 'str'], // ':id' => 36, PDO::PARAM_STR
]);