1. Go to this page and download the library: Download sugiphp/database-ext 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/ */
sugiphp / database-ext example snippets
use SugiPHP\DatabaseExt\PdoExt;
$db = new PdoExt('sqlite:/path/to/database.sqlite');
$db->exec("INSERT INTO users (name) VALUES ('Ivan')");
$sth = $db->prepare('SELECT * FROM users WHERE id = ?');
$sth->execute([1]);
use SugiPHP\DatabaseExt\PdoExt;
use Psr\EventDispatcher\EventDispatcherInterface;
$db = new PdoExt('sqlite:/path/to/database.sqlite');
$db->setEventDispatcher($dispatcher);
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use SugiPHP\DatabaseExt\Event\{
BeforeExec, BeforeQuery, BeforeExecute,
AfterExec, AfterQuery, AfterExecute,
ExecError, QueryError, ExecuteError,
Rejected,
};
use SugiPHP\DatabaseExt\PdoExt;
class QueryProfiler implements EventDispatcherInterface
{
public function __construct(private LoggerInterface $logger)
{
}
public function dispatch(object $event): object
{
match (true) {
$event instanceof BeforeExec,
$event instanceof BeforeQuery,
$event instanceof BeforeExecute
=> $event->setAttribute('start', microtime(true)),
$event instanceof AfterExec,
$event instanceof AfterQuery,
$event instanceof AfterExecute
=> $this->logger->info('{ms}ms {sql}', $this->context($event)),
$event instanceof ExecError,
$event instanceof QueryError,
$event instanceof ExecuteError
=> $this->logger->error(
'{ms}ms {sql} failed: {error}',
$this->context($event) + ['error' => $event->getException()->getMessage()]
),
$event instanceof Rejected
=> $this->logger->warning('rejected (state={state}) {sql}', [
'state' => $event->getState(),
'sql' => $event->getQueryString(),
]),
default => null,
};
return $event;
}
private function context(AfterExec|AfterQuery|AfterExecute|ExecError|QueryError|ExecuteError $event): array
{
$start = $event->getBefore()->getAttribute('start');
return [
'sql' => $event->getQueryString(),
'ms' => round((microtime(true) - $start) * 1000, 1),
];
}
}
$db = new PdoExt('sqlite:/path/to/database.sqlite', options: [
'dispatcher' => new QueryProfiler($logger),
]);
$db->exec("INSERT INTO users (name) VALUES ('Ivan')"); // logged with its duration
use SugiPHP\DatabaseExt\PdoExt;
$db = new PdoExt('sqlite:/path/to/database.sqlite');
$db->setState(PdoExt::STATE_READ_ONLY); // or STATE_READ_WRITE / STATE_UNAVAILABLE
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.