How can the error reporting in PHP be optimized to better identify and troubleshoot issues with database operations?

When working with database operations in PHP, it is important to optimize error reporting to better identify and troubleshoot issues. One way to do this is by setting the error reporting level to include warnings and errors related to database operations. This can help in quickly identifying issues such as connection problems, query errors, or data retrieval problems.

// Set error reporting level to include warnings and errors
error_reporting(E_ALL);
ini_set('display_errors', 1);

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

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

// Check for connection errors
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Perform database operations here