Skip to content
PHP Logo

How can error reporting be effectively used to debug PHP scripts that involve database operations?

To effectively debug PHP scripts that involve database operations, error reporting can be used to display any errors that occur during the execution of the script. By enabling error reporting and displaying detailed error messages, developers can quickly identify and fix any issues with database queries or connections.

php
// Enable error reporting for debugging
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