How can the var_dump function be used to troubleshoot PHP code that is not returning the expected results from a MySQL query?

When PHP code is not returning the expected results from a MySQL query, the var_dump function can be used to inspect the data returned by the query. By using var_dump, you can see the structure and contents of the data, which can help identify any issues with the query or how the results are being processed in the code.

// Perform MySQL query
$query = "SELECT * FROM table_name";
$result = mysqli_query($connection, $query);

// Check if query was successful
if($result) {
    // Fetch and display results
    while($row = mysqli_fetch_assoc($result)) {
        var_dump($row); // Inspect the data returned by the query
        // Process the data as needed
    }
} else {
    echo "Error: " . mysqli_error($connection);
}