PHP code example of chrlb / pdao

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

    

chrlb / pdao example snippets




use Chrlb\PhpDao\PHPDAO;
use PDO;

$conn = new PDO("mysql:host=localhost;dbname=your_db", "your_user", "your_password");
$dao = new PHPDAO($conn);

// Register a named statement
$dao->prepareStatement("getUsers", "SELECT * FROM tbl_users");

// Run it (SELECT)
$users = $dao->executeQuery("getUsers");

// Register and run an UPDATE
$dao->prepareStatement("updateUserName", "UPDATE tbl_users SET name = ? WHERE id = ?");
$affectedRows = $dao->executeUpdate("updateUserName", ["NewName", 1]);

try {
    $dao->executeQuery("getUsers");
} catch (OutOfBoundsException $e) {
    echo "Statement not found: " . $e->getMessage();
}

class LoggingPHPDAO extends PHPDAO {
    public function executeQuery(string $title, array $params = []): array {
        error_log("Running query: $title");
        return parent::executeQuery($title, $params);
    }
}
terminal
composer