PHP code example of stk2k / power-pdo

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

    

stk2k / power-pdo example snippets


class UserEntity
{
    public $ID;
    public $deleted;
    public $user_name;
    public $nickname;
    public $email;
}

use Stk2k\PowerPDO\PowerPDO;

$dsn = 'mysql:dbname=mydatabase;host=localhost';
$user = 'myuser';
$password = 'mypass';

$ppdo = new PowerPDO(new PDO($dsn, $user, $password));
or 
$ppdo = PowerPDO::make($dsn, $user, $password);

use Stk2k\PowerPDO\PowerPDO;

$dsn = 'sqlite:/path/to/dbfile_of_sqlite';
$ppdo = new PowerPDO(new PDO($dsn));
or 
$ppdo = PowerPDO::make($dsn);

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// monolog
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));

$ppdo = new PowerPDO(new PDO($dsn), $log);
or 
$ppdo = PowerPDO::make($dsn)->logger($log);


// array style
$users = PowerPDO::make($dsn)
    ->select("ID, user_name, nickname, email")
    ->from("users")
    ->where("deleted = 0")
    ->getAll();
foreach($users as $u){
    $uid = $u['ID'];
    $name = $u['user_name'];
    echo "[$uid]$name";
}

// entity style
$users = PowerPDO::make($dsn)
    ->select("ID, user_name, nickname, email")
    ->from("users")
    ->where("deleted = 0")
    ->getAll(UserEntity::class);

foreach($users as $u){
    $uid = $u->ID;
    $name = $u->user_name;
    echo "[$uid]$name";
}


$users = PowerPDO::make($dsn)
    ->select("ID, user_name, nickname, email")
    ->from("users")
    ->where("nickname LIKE :nickname")
    ->bind(['nickname' => '%Bi%'])
    ->getAll();

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$users = PowerPDO::make($dsn)
    ->count()
    ->from("users")
    ->where("deleted = 0")
    ->get();

use Stk2k\PowerPDO\Transaction;
use Stk2k\PowerPDO\exception\TransactionException;

try{
    $tr = new Transaction($pdo);
    $tr->begin();
    // execute UPDATE/DELETE/INSERT SQL here
    $tr->commit();
}
catch(TransactionException $ex){
    $tr->rollback();
}

// array style
PowerPDO::make($dsn)
    ->insert()
    ->into("users", "ID, user_name, nickname, email")
    ->values(
        "ID" => "123",
        "user_name" => "hanako", 
        "nickname" => "hana", 
        "email" => "[email protected]"
    )
    ->execute();

// entity style
$new_user = new UserEntity();

$new_user->ID = 123;
$new_user->user_name = 'hanako';
$new_user->nickname = 'hana';
$new_user->email = '[email protected]';

PowerPDO::make($dsn)
    ->insert()
    ->into("users")
    ->values($new_user)
    ->execute();

// literal style
PowerPDO::make($dsn)
    ->update("users")
    ->set("user_name", "hanako2")
    ->set("email", "[email protected]")
    ->where("ID = :ID", ['ID'=>1])
    ->execute();

// array style
PowerPDO::make($dsn)
    ->update("users")
    ->values([
            'user_name' => 'hanako2',
            'email' => '[email protected]',
        ])
    ->where("ID = :ID", ['ID'=>1])
    ->execute();

// entity style
$new_user = new UserEntity();

$new_user->user_name = 'hanako2';
$new_user->nickname = 'hana2';
$new_user->email = '[email protected]';

PowerPDO::make($dsn)
    ->update("users")
    ->values($new_user)
    ->where("ID = :ID", ['ID'=>1])
    ->execute();

use Stk2k\PowerPDO\PowerPDO;

// literal style
PowerPDO::make($dsn)
    ->delete()
    ->from("users")
    ->where("email = '[email protected]'")
    ->execute();

// placeholder style
PowerPDO::make($dsn)
    ->delete()
    ->from("users")
    ->where("email = :email",['email' => '[email protected]'])
    ->execute();