What are the potential pitfalls of using nested while loops in PHP for MySQL queries?

Using nested while loops for MySQL queries can lead to performance issues, especially with large datasets. It can result in unnecessary iterations and slow down the execution of the script. To avoid this, consider using JOINs in your SQL query to retrieve the desired data in a single result set, which can improve performance significantly.

// Example of using JOIN in SQL query instead of nested while loops
$sql = "SELECT orders.order_id, customers.customer_name
        FROM orders
        JOIN customers ON orders.customer_id = customers.customer_id";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        // Process each row
    }
} else {
    echo "0 results";
}