What are common pitfalls when using nested while loops in PHP queries?

Common pitfalls when using nested while loops in PHP queries include accidentally creating an infinite loop, inefficient code execution, and potential memory consumption issues. To avoid these pitfalls, it's important to carefully structure the loops and ensure proper termination conditions are set.

// Example of using nested while loops with proper termination conditions

$query1 = mysqli_query($connection, "SELECT * FROM table1");
while($row1 = mysqli_fetch_assoc($query1)) {
    $query2 = mysqli_query($connection, "SELECT * FROM table2 WHERE id = ".$row1['id']);
    while($row2 = mysqli_fetch_assoc($query2)) {
        // Process data from table2
    }
}