PHP code example of comphp / database

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

    

comphp / database example snippets




use Neuron\Database\DatabaseManager;
use Neuron\Database\Drivers\MysqlDatabaseDriver;
use Neuron\Extensibility\ExtensionStore;
use Neuron\Extensibility\InstantiatorInterface;
use Psr\Log\LoggerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;

// Imagine these are created or injected by your DI container
/** @var LoggerInterface $logger */
/** @var InstantiatorInterface $instantiator */
/** @var EventDispatcherInterface $eventDispatcher */

$extensions = new ExtensionStore($instantiator, $logger, $eventDispatcher);
$database = new DatabaseManager($logger, $extensions, $eventDispatcher);

// Create a new connection
$database->connect('_default_', MysqlDatabaseDriver::class, [
    'database'  => 'my_database',
    'host'      => 'localhost',
    'username'  => 'root',
    'password'  => 'root',
]);

// Fetch rows
$rows = $database->fetchAll('SELECT * FROM users');

foreach ($rows as $row) {
    echo "User: " . $row['name'] . "\n";
}

$database->connect('mysql_main', MysqlDatabaseDriver::class, [
    'database' => 'main_db',
    'host' => 'localhost',
    'username' => 'root',
    'password' => ''
]);

$database->connect('sqlite_backup', AliasDatabaseDriver::class, [
    'database' => $database,
    'target' => 'mysql_main'
]);

// Use them
$backupRows = $database->fetchAll('SELECT * FROM logs', [], 'sqlite_backup');

$database->transaction(function (\Neuron\Database\DatabaseDriverInterface $driver) {
    $driver->execute('INSERT INTO accounts ...');
    $driver->execute('UPDATE ledger ...');
});

$database->enableProfiling();
// All queries now dispatch QueryExecutedEvent with timing info

use Neuron\Database\AbstractDatabaseDriver;
use Neuron\Database\DatabaseDriver;
use Neuron\Database\DatabaseDriverInterface;

#[DatabaseDriver]
class YourCustomDriver extends AbstractDatabaseDriver implements DatabaseDriverInterface
{
    // implement count(), fetchOne(), fetchAll(), etc.
}

// Then register
$extensions->getRegistry()->register(DatabaseDriver::class, YourCustomDriver::class);