How can error reporting be effectively implemented in PHP scripts to troubleshoot issues with MySQLi?

When troubleshooting issues with MySQLi in PHP scripts, error reporting can be effectively implemented by enabling error reporting and displaying detailed error messages. This can help identify the root cause of the problem and aid in resolving it quickly.

// Enable error reporting for MySQLi
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

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

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