1. Go to this page and download the library: Download faster-php/db 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/ */
faster-php / db example snippets
use FasterPhp\Db\Db;
$db = new Db(
dsn: 'mysql:host=localhost;dbname=mydb',
username: 'myuser',
password: 'mysecret',
);
$stmt = $db->prepare('SELECT * FROM users WHERE id = :id');
$stmt->execute([':id' => 1]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
use FasterPhp\Db\Db;
use FasterPhp\Db\Reconnect\DefaultStrategy;
$strategy = new DefaultStrategy(
maxAttempts: 3, // Try up to 3 times (default: 1)
baseDelayMs: 100, // Start with 100ms delay (default: 100)
backoffMultiplier: 2.0 // Double the delay each attempt (default: 2.0)
);
$db = new Db(
dsn: 'mysql:host=localhost;dbname=mydb',
username: 'myuser',
password: 'mysecret',
reconnectStrategy: $strategy
);
$db->beginTransaction();
$db->exec('INSERT INTO users (name) VALUES ("Alice")');
// If connection is lost here, DbException is thrown instead of reconnecting
// This prevents silent data loss
use Psr\Log\LoggerInterface;
$db->setLogger($logger);
// Reconnect events are logged at WARNING level with DSN and error details
$stmt1 = $db->prepare('SELECT * FROM users WHERE id = :id');
$stmt2 = $db->prepare('SELECT * FROM users WHERE id = :id');
// $stmt1 === $stmt2
$db->clearStatementCache();
use FasterPhp\Db\ConnectionManager;
use FasterPhp\Db\Config\ArrayProvider;
$config = new ArrayProvider([
'main' => [
'dsn' => 'mysql:host=127.0.0.1;dbname=main_db',
'username' => 'root',
'password' => '',
'options' => [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
],
],
'analytics' => [
'dsn' => 'mysql:host=127.0.0.1;dbname=analytics',
'username' => 'readonly',
'password' => '',
],
]);
$manager = new ConnectionManager($config);
// Get a shared Db instance
$db = $manager->get('main');
interface ProviderInterface {
public function get(string $name): ?array;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.