What are the potential risks of not handling error messages properly in PHP scripts that interact with a MySQL database?

If error messages are not handled properly in PHP scripts that interact with a MySQL database, sensitive information about the database structure or queries could be exposed to potential attackers. This could lead to security vulnerabilities such as SQL injection attacks. To prevent this, error messages should be handled securely by displaying generic error messages to users and logging detailed error information for developers to troubleshoot.

// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    // Display generic error message to user
    echo "Oops! Something went wrong. Please try again later.";

    // Log detailed error information for developers
    error_log("Connection failed: " . $conn->connect_error);
    exit();
}