What are best practices for debugging PHP code related to database queries?

When debugging PHP code related to database queries, it is important to check for errors in the query syntax, connection to the database, and data retrieval process. One common practice is to use error handling functions like mysqli_error() to display any errors that may occur during the query execution. Additionally, using var_dump() or print_r() to inspect the query results can help identify any issues with data retrieval.

// Example of debugging PHP code related to database queries
$conn = mysqli_connect("localhost", "username", "password", "database");

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);

if (!$result) {
    die("Query failed: " . mysqli_error($conn));
}

while ($row = mysqli_fetch_assoc($result)) {
    var_dump($row);
}

mysqli_close($conn);