PHP code example of riculum / php-pdo

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

    

riculum / php-pdo example snippets




use Database\Core\Database as DB;

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

try {
    $user = DB::select('SELECT * FROM my_table WHERE firstname = ?', ['John']);
} catch (PDOException $e) {
    echo $e->getMessage();
}

try {
    $user = DB::single('SELECT * FROM my_table WHERE firstname = ?', ['John']);
} catch (PDOException $e) {
    echo $e->getMessage();
}

try {
    $id = DB::insert('INSERT INTO my_table (firstname, lastname) VALUES (?,?)', ['John', 'Doe']);
} catch (PDOException $e) {
    echo $e->getMessage();
}

$data = [
    'firstname' => 'John',
    'lastname' => 'Doe'
];

try {
    $id = DB::insertAssoc('my_table', $data);
} catch (PDOException $e) {
    echo $e->getMessage();
}

try {
    DB::update('UPDATE my_table SET firstname = ? WHERE lastname = ?', ['John', 'Doe']);
} catch (PDOException $e) {
    echo $e->getMessage();
}

$data = [
    'firstname' => 'John',
    'lastname' => 'Doe'
];

$condition = [
    'key' => 'id',
    'operator' => '=',
    'value' => 1
];

try {
    $id = DB::updateAssoc('my_table', $data, $condition);
} catch (PDOException $e) {
    echo $e->getMessage();
}

try {
    DB::delete('DELETE FROM my_table WHERE firstname = ?', ['John']);
} catch (PDOException $e) {
    echo $e->getMessage();
}

try {
    DB::statement('DROP TABLE my_table');
} catch (PDOException $e) {
    echo $e->getMessage();
}

$data = [
    'firstname' => 'John',
    'lastname' => 'Doe'
];

try {
    DB::beginTransaction();
    $id = DB::insertAssoc('my_table', $data);
    DB::update('UPDATE my_table SET firstname = ?, lastname = ? WHERE id = ?', ['Jane', 'Doe', $id]);
    DB::commit();
} catch (PDOException $e) {
    echo $e->getMessage();
}
bash
composer