How can the print_r() function help in debugging PHP code related to database queries?
The print_r() function can help in debugging PHP code related to database queries by displaying the contents of variables, arrays, and objects. By using print_r() on the result of a database query, you can see the data that is being retrieved from the database, helping you identify any issues with the query or the data itself.
// Example of using print_r() to debug a database query
$query = "SELECT * FROM users";
$result = mysqli_query($connection, $query);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
print_r($row);
}
} else {
echo "Error: " . mysqli_error($connection);
}