How can a developer effectively utilize error reporting functions like error_reporting and mysql_error to troubleshoot issues in PHP scripts interacting with a database?

When developing PHP scripts that interact with a database, it is important to utilize error reporting functions like error_reporting and mysql_error to troubleshoot issues. By enabling error reporting and checking for MySQL errors, developers can easily identify and resolve any issues that may arise during database interactions.

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

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

$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check for connection errors
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Perform database operations
$sql = "SELECT * FROM table";
$result = mysqli_query($conn, $sql);

// Check for query errors
if (!$result) {
    die("Error: " . mysqli_error($conn));
}

// Process query results
while ($row = mysqli_fetch_assoc($result)) {
    // Process each row
}

// Close the connection
mysqli_close($conn);