What are some potential pitfalls when using arrays in PHP to store data from a MySQL database?
One potential pitfall when using arrays in PHP to store data from a MySQL database is the risk of memory exhaustion if the array becomes too large. To prevent this, you can limit the number of rows fetched from the database at a time and process them in batches.
// Limit the number of rows fetched from the database at a time
$limit = 1000;
$offset = 0;
while (true) {
$result = $mysqli->query("SELECT * FROM table LIMIT $limit OFFSET $offset");
if ($result->num_rows == 0) {
break;
}
while ($row = $result->fetch_assoc()) {
// Process each row here
}
$offset += $limit;
}