How can error reporting be used effectively to debug PHP code that interacts with a MySQL database?

To effectively debug PHP code that interacts with a MySQL database, error reporting can be used to identify and address any issues that may arise during the execution of the code. By enabling error reporting in PHP, any errors or warnings that occur can be displayed, providing valuable information on where the problem lies. This can help developers quickly pinpoint and resolve issues in their code.

// Enable error reporting in PHP
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Your PHP code that interacts with MySQL database
// Example: Connecting to a MySQL database
$servername = "localhost";
$username = "username";
$password = "password";

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

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}