A database query or connection fails using PDO and the exception is not caught in a try/catch block.
When using PHP Data Objects (PDO) with error mode set to PDO::ERRMODE_EXCEPTION, database failures raise PDOException. If not caught, it halts script execution and might expose database credentials in the stack trace.
Incorrect database host, port, or credentials, SQL syntax mistakes in queries, or missing database drivers.
1<?php2// Bug: Incorrect database password, throwing uncaught connection exception3$dsn = "mysql:host=localhost;dbname=test_db;charset=utf8mb4";4$pdo = new PDO($dsn, "db_user", "wrong_password"); // Throws PDOException1<?php2$dsn = "mysql:host=localhost;dbname=test_db;charset=utf8mb4";3 4try {5 // Fix: Wrap connection and queries in try-catch blocks6 $pdo = new PDO($dsn, "db_user", "correct_password");7 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);8 echo "Database connected successfully.";9} catch (PDOException $e) {10 // Log error internally and show clean error message to user11 error_log("DB Connection Error: " . $e->getMessage());12 die("Could not connect to the database. Please try again later.");13}Simulate standard system builds to trigger compiler trace records and track memory crashes locally.
The connection attempt fails due to bad credentials. Since the PDO constructor call is not wrapped in a try/catch block, PHP crashes with a Fatal error, printing the database username and host in the error trace.