PHP 8 · Web, security and database

Connect with PDO

Connect to MySQL with exceptions on errors. Use real credentials from the environment.

$pdo = new PDO(
    'mysql:host=127.0.0.1;dbname=app;charset=utf8mb4',
    getenv('DB_USER'),
    getenv('DB_PASSWORD'),
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]
);

Official PHP 8 documentation

More web, security and database commands

All web, security and database commands · PHP 8 cheatsheet

Was this page helpful?