PHP code example of semperton / database

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

    

semperton / database example snippets


use Semperton\Database\Connection\PDOConnection;

$connection = new PDOConnection('dsn', null, null, [
	PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

interface ConnectionInterface
{
	public function execute(string $sql, ?array $params = null): bool;
	public function fetchRow(string $sql, ?array $params = null): ?array;
	public function fetchColumn(string $sql, ?array $params = null, int $column = 0): Generator;
	public function fetchAll(string $sql, ?array $params = null): Generator;
	public function fetchResult(string $sql, ?array $params = null): ResultSetInterface;
	public function fetchValue(string $sql, ?array $params = null);
	public function inTransaction(): bool;
	public function beginTransaction(): bool;
	public function commit(): bool;
	public function rollBack(): bool;
	public function lastInsertId(): int;
	public function affectedRows(): int;
}

$users = $connection->fetchAll('select * from user limit :limit', ['limit' => 5]);

$firstUser = $users->first();

foreach($users as $user){
	// ...
}

$userCount = $users->count();
$userArray = $users->toArray();