PHP code example of tokkerbaz / phpsql-lint

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

    

tokkerbaz / phpsql-lint example snippets


use PhpSqlLint\Analyzer;
use PhpSqlLint\Query;
use PhpSqlLint\Report\CliReporter;

$analyzer = new Analyzer(); // uses the built-in rule set by default

$findings = $analyzer->analyze(
    new Query("SELECT * FROM users WHERE email LIKE '%gmail.com'")
);

echo (new CliReporter())->render($findings);

use PhpSqlLint\Explain\ExplainRunner;
use PhpSqlLint\Query;

$runner = new ExplainRunner($pdo); // any PDO connection, mysql or pgsql

$result = $runner->run(new Query('SELECT * FROM orders WHERE customer_id = ?', [42]));

if ($result->isFullScan()) {
    echo "Full scan estimated at {$result->estimatedRows} rows — consider an index.\n";
}

use PhpSqlLint\Collector\QueryLogCollector;
use PhpSqlLint\Query;

$collector = new QueryLogCollector();

// wire this into your PDO wrapper / ORM query event
$collector->record(new Query($sql, $bindings, durationMs: $elapsed));

// at the end of the request:
foreach ($collector->detectNPlusOne(threshold: 5) as $shape => $queries) {
    echo "Possible N+1: '{$shape}' ran " . count($queries) . " times\n";
}

use PhpSqlLint\Collector\QueryLogCollector;
use PhpSqlLint\Debugger\PageDebugger;

$debugger = new PageDebugger(app(QueryLogCollector::class));
Log::info('SQL findings', ['findings' => $debugger->findings()]);

use PhpSqlLint\Debugger\TrackedPdo;
use PhpSqlLint\Debugger\PageDebugger;

// swap your normal `new PDO(...)` for this — everything else about using
// the connection (prepare, execute, fetch) stays exactly the same
$pdo = new TrackedPdo($dsn, $user, $pass, $options);

// ... your app runs, executing queries against $pdo as normal ...

$debugger = new PageDebugger($pdo->collector());

// in dev/staging only — never enable against production traffic,
// since the panel 

$debugger->summary();          // ['query_count' => ..., 'total_duration_ms' => ..., ...]
$debugger->findings();         // Finding[]
$debugger->nPlusOneGroups();   // array<string, Query[]>
$debugger->slowQueries(100.0); // Query[] slower than 100ms, slowest first

use PhpSqlLint\Rules\AbstractRule;
use PhpSqlLint\Query;
use PhpSqlLint\Finding;

final class NoOrderByRandRule extends AbstractRule
{
    public function id(): string { return 'no-order-by-rand'; }

    public function check(Query $query): array
    {
        if (!preg_match('/order by rand\(\)/i', $query->raw())) {
            return [];
        }

        return [Finding::for(
            rule: $this,
            query: $query,
            message: 'ORDER BY RAND() forces a full scan and a temp sort on most engines.',
            suggestedFix: 'Use a pre-computed random key column, or sample IDs separately.',
        )];
    }
}