Reputation: 2336
What is alternative to fetchall
for real time loop?
Consider this:
$query = $db->prepare("SELECT * FROM record WHERE status = 0");
$query->execute();
$Record = $query->fetchall(PDO::FETCH_ASSOC);
foreach ($Record as $row) {
echo $row['name'];
sleep(5)
}
While its looping and echo
'ing, I updated status = 1
from the console but it will still show the record which it shouldn't.
Upvotes: 0
Views: 2969
Reputation:
The results of the query are calculated once, when you run the query. If you need to get all new results with status = 0
, you'll need to rerun the query.
Upvotes: 1
Reputation: 6814
How about a simple fetch()
: https://www.php.net/manual/en/pdostatement.fetch.php
$query = $db->prepare("SELECT * FROM record WHERE status = 0");
$query->execute();
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo $row['name'];
sleep(5)
}
Upvotes: 5