user622378
user622378

Reputation: 2336

Alternative to fetchall (PDO)?

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

Answers (2)

user149341
user149341

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

Bart
Bart

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

Related Questions