PHP code example of victorvolpe / php-pdo-wrapper

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

    

victorvolpe / php-pdo-wrapper example snippets


use VictorVolpe\PhpPdoWrapper\DB;

$db = new DB(
    dsn: 'mysql:host=localhost;dbname=testdb;charset=utf8mb4',
    user: 'your_username',
    pass: 'your_password'
);

$users = $db->query("SELECT * FROM users WHERE status = :status", [
    'status' => 'active'
]);

$user = $db->row("SELECT * FROM users WHERE id = :id", [
    'id' => 1
]);

$emails = $db->column("SELECT email FROM users WHERE status = :status", [
    'status' => 'active'
]);

$email = $db->single("SELECT email FROM users WHERE id = :id", [
    'id' => 1
]);

$newUserId = $db->insert('users', [
    'username' => 'newuser',
    'email' => '[email protected]',
    'status' => 'active'
]);

$rowsAffected = $db->update('users', [
    'status' => 'inactive'
], "last_login < :date", [
    'date' => '2024-01-01'
]);

$rowsDeleted = $db->delete('users', "status = :status", [
    'status' => 'inactive'
]);

$db->begin();

try {
    $db->query("UPDATE accounts SET balance = balance - :amount WHERE id = :id", [
        'amount' => 100,
        'id' => 1
    ]);

    $db->query("UPDATE accounts SET balance = balance + :amount WHERE id = :id", [
        'amount' => 100,
        'id' => 2
    ]);

    $db->commit();
} catch (Exception $e) {
    $db->rollback();
    throw $e;
}

composer