What are the security risks associated with directly outputting MySQL errors in PHP applications?

Directly outputting MySQL errors in PHP applications can expose sensitive information about your database structure and potentially provide attackers with valuable information to exploit. To mitigate this risk, it is recommended to log the errors instead of displaying them directly to users.

// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Check connection
if ($mysqli->connect_error) {
    // Log the error instead of displaying it directly
    error_log("Connection failed: " . $mysqli->connect_error);
    die("An error occurred. Please try again later.");
}