PHP code example of tusharkhan / db-error-explainer-core

1. Go to this page and download the library: Download tusharkhan/db-error-explainer-core 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/ */

    

tusharkhan / db-error-explainer-core example snippets


use TusharKhan\DbErrorExplainer\Explainer;

$explainer = new Explainer($pdo);

try {
    $pdo->exec("INSERT INTO orders (user_id) VALUES (999)");
} catch (PDOException $e) {
    $diagnosis = $explainer->explain($e);

    echo $diagnosis->format();
}

$explainer = new Explainer(null, ['driver' => 'mysql']);   // mysql, mariadb, pgsql or sqlite

$explainer = new Explainer($pdo, [
    'driver' => null,                  // null = detect; or 'mysql', 'mariadb', 'pgsql', 'sqlite', or a DriverInterface
    'allow_introspection' => true,     // read-only schema lookups for more specific explanations
    'redact_sensitive_data' => true,   // hide values of keys like password, token, api_key
    'custom_rules' => [],              // see below
], $psrLogger);                        // optional PSR-3 logger: every diagnosis is logged at info level

$explainer = new Explainer($pdo, [
    'custom_rules' => [
        [
            'id' => 'orders_status_check',
            'error_code' => [3819],                       // driver codes, and/or
            'sqlstate' => ['HY000'],                      // SQLSTATEs
            'message_regex' => '/orders_status_check/',   // prefers this rule when several match
            'ustomRule([...]);   // or later