What are some potential pitfalls of using while loops in PHP when querying a database?

Using while loops in PHP when querying a database can potentially lead to performance issues if the result set is large, as it retrieves all rows at once and stores them in memory. To avoid this, consider using a LIMIT clause in your SQL query to limit the number of rows returned, or use pagination to fetch data in smaller chunks.

// Example of using LIMIT clause in SQL query to limit the number of rows returned
$query = "SELECT * FROM table_name LIMIT 10";
$result = mysqli_query($connection, $query);

while ($row = mysqli_fetch_assoc($result)) {
    // Process each row here
}