How can PHP developers effectively troubleshoot MySQL errors in their code?

To effectively troubleshoot MySQL errors in PHP code, developers can enable error reporting, check for syntax errors in SQL queries, use error handling techniques such as try-catch blocks, and utilize tools like phpMyAdmin to debug queries and view database information.

<?php
// Enable error reporting
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);
}

// Perform SQL query
$query = "SELECT * FROM users";
$result = $mysqli->query($query);

// Check for query errors
if (!$result) {
    die("Error executing query: " . $mysqli->error);
}

// Fetch results
while ($row = $result->fetch_assoc()) {
    echo $row['username'] . "<br>";
}

// Close connection
$mysqli->close();
?>