PHP code example of maduser / argon-database

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

    

maduser / argon-database example snippets


use Maduser\Argon\Database\QueryClient;
use Maduser\Argon\Database\PDO\MySQL\MySQLConfig;
use Maduser\Argon\Database\PDO\LazyConnectionProvider;

$provider = new LazyConnectionProvider(
    new MySQLConfig('127.0.0.1', 3306, 'argondb', 'argon', 'secret')
);

$db = new QueryClient($provider);

new PostgresConfig('127.0.0.1', 5432, 'argondb', 'argon', 'secret');
new SqliteConfig(__DIR__ . '/mydb.sqlite');

$rows = $db
    ->query('SELECT * FROM users WHERE active = :active', ['active' => true])
    ->fetchAll();

$user = $db
    ->query('SELECT * FROM users WHERE id = :id', ['id' => 42])
    ->fetchOne();

$db->query('UPDATE users SET banned = true WHERE id = :id', ['id' => 666])
   ->execute();

$users = $db
    ->file(__DIR__ . '/queries/select_active_users.sql', ['limit' => 10])
    ->fetchAll();

use Maduser\Argon\Database\Contracts\RowMapper;

/**
 * @implements RowMapper<User>
 */
final class User implements RowMapper
{
    public function __construct(
        public readonly int $id,
        public readonly string $email
    ) {}

    public static function map(array $row): self
    {
        return new self(
            id: (int) $row['id'],
            email: (string) $row['email']
        );
    }
}

/** @var list<User> */
$users = $db
    ->query('SELECT * FROM users')
    ->fetchAllTo(User::class);

/** @var User|null */
$user = $db
    ->query('SELECT * FROM users WHERE id = :id', ['id' => 1])
    ->fetchOneTo(User::class);

$result = $db->transaction(function (QueryClient $trx) {
    $trx->query(...)->execute();
    return 'done';
});