PHP code example of thephpleague / database

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

    

thephpleague / database example snippets

 php
$config = [
    'host'      => 'localhost',
    'port'      => 3306,
    'database'  => 'master_db',
    'username'  => 'root',
    'password'  => '',
];

$connection = new League\Database\ConnectionManager('core', $config);
 php
use League\Database\BulkSql\BulkReplace;
use League\Database\BulkSql\BulkDelete;

$db = $connection->getMasterConnection();

$bulkReplace = new BulkReplace($db, 'offers');
$bulkReplace->setItemsPerQuery(500);
$bulkDelete = new BulkDelete($db, 'offers');
$bulkDelete->setItemsPerQuery(1000);

try {
    $db->beginTransaction();
    
    foreach ($offers as $offer) {
        $flag = $offer['deltaStatus'] ?? null;
        
        switch ($delta) {
            case 'REMOVE':
                $bulkDelete->add(['id' => $data['id']]);
                break;
            case 'ADD':
                $bulkReplace->add($data);
                break;
            default:
                $logger->notice("Unsupported delta flag \"{$flag}\"";
        }
    }

    $bulkReplace->finish();
    $bulkDelete->finish();
    
    $db->commit();
    
    $insertsCount = $bulkReplace->getInsertedCount();
    $updatesCount = $bulkReplace->getReplacedCount();
    $deletesCount = $bulkDelete->getAffectedCount();
} catch (\PDOException $e) {
    $db->rollBack();
}