PHP code example of dawidgorecki / dbal

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

    

dawidgorecki / dbal example snippets


use Reven\DBAL\Configuration\DBConfig;
use Reven\DBAL\Configuration\DSN;

$dsn = new DSN(DSN::DRIVER_PGSQL, 'my_db');
// $dsn = new DSN(DSN::DRIVER_MYSQL, 'my_db', 'localhost', DSN::PORT_MYSQL);

$config = new DBConfig($dsn, 'username', 'passwd');
// $config = new DBConfig($dsn, 'username', 'passwd', 'utf8', true);

use Reven\DBAL\ConnectionManager;
use Reven\DBAL\DBALDatabase;
use Reven\DBAL\Exceptions\DBALException;

try {
    ConnectionManager::createConnection($config);
    // ConnectionManager::createConnection($config1, 'db1', PDO::FETCH_ASSOC);
    // ConnectionManager::createConnection($config2, 'db2', PDO::FETCH_OBJ);
} catch (DBALException $e) {
    die($e);
}

$dbal = new DBALDatabase(ConnectionManager::getConnection());
// $dbal = new DBALDatabase(ConnectionManager::getConnection('db1'));

use Reven\DBAL\DatabaseFactory;
use Reven\DBAL\DBALDatabase;
use Reven\DBAL\Exceptions\DBALException;

try {
    $pdo = DatabaseFactory::getConnection($config, PDO::FETCH_ASSOC);
} catch (DBALException $e) {
    die($e);
}

$dbal = new DBALDatabase($pdo);

$dbal = new DBALDatabase($pdo, false);

$dbal->getLastError();
$dbal->getQueryString();

$dbal->getPDO();

$dbal->startTransaction();

$dbal->commit();

$dbal->rollback();

$users = $dbal->fetchAll("SELECT * FROM users");
    
/* 
Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Dawid
            [age] => 31
        )
)
*/

$user = $dbal->fetchFirst("SELECT * FROM users ORDER BY id", [], PDO::FETCH_ASSOC);
    
/*
Array
(
    [id] => 1
    [name] => Dawid
    [age] => 31
)
*/

$user = $dbal->fetchArray("SELECT * FROM users ORDER BY id");
    
/*
Array
(
    [0] => 1
    [1] => Dawid
    [2] => 31
)
*/

$user = $dbal->fetchAssoc("SELECT * FROM users WHERE name = ?", ["Dawid"]);
    
/*
Array
(
    [id] => 1
    [name] => Dawid
    [age] => 31
)
*/

$user = $dbal->fetchColumn("SELECT * FROM users WHERE id = ?", [1], 1);

// Dawid

$dbal->delete('users', ["id" => 1]);
$dbal->delete('users', ["name" => "Dawid"]);

$dbal->insert('users', ["name" => "John", "age" => 35]);

$dbal->update('users', ["name" => "New John", "age" => 40], ["id" => 15]);

$stmt = $dbal->executeQuery("SELECT * FROM users");

while ($user = $stmt->fetchObject()) {
    print_r($user);
}

/*
stdClass Object
(
    [id] => 1
    [name] => Dawid
    [age] => 31
)
stdClass Object
(
    [id] => 15
    [name] => New John
    [age] => 40
)
*/

$rows_affected = $dbal->updateQuery("DELETE FROM users WHERE name = ?", ["New John"]);

$stmt = $dbal->prepare("SELECT * FROM users WHERE name LIKE 'D%'");
$stmt->execute();

while ($user = $stmt->fetch(PDO::FETCH_NUM)) {
    print_r($user);
}

/*
Array
(
    [0] => 1
    [1] => Dawid
    [2] => 31
)
Array
(
    [0] => 12
    [1] => Dominik
    [2] => 1
)
*/

$quoted = $dbal->quote("Hello", PDO::PARAM_STR);

$last_id = $dbal->lastId();



namespace Reven\DBAL;

class User extends ActiveRecord
{
}

ConnectionManager::createConnection($config, 'active_record');

User::setConnectionName('active_record');

User::setTableName('employers');

// INSERT INTO users(name,email) VALUES('John','[email protected]')  
$user = new User();
$user->setName("John");
$user->setEmail("[email protected]");
$user->save();

// SELECT * FROM users WHERE id=1 LIMIT 1
$user = User::findById(1);
echo $user->getName();

// SELECT * FROM users
$users = User::findAll();
foreach ($users as $user) {
    echo $user->getName();
}

// SELECT * FROM users WHERE email='[email protected]'
$users = User::findByQuery("SELECT * FROM users WHERE email=:email", [":email" => "[email protected]"]);
echo $users[0]->getName();

// UPDATE users SET name='Edwin' WHERE id=1
$user = User::findById(1);
$user->setName("Edwin");
$user->save();

// DELETE FROM users WHERE id=1
$user = User::findById(1);
$user->delete();