Reputation: 758
I have a table containing 3 rows. I am trying to loop through all the rows but I am not getting the right amount of rows.
My code is as follow:
$result1_prepare = $DB->prepare("SELECT * FROM table");
$result1_prepare->execute();
$num = $result1_prepare->fetchColumn();
$result1 = $result1_prepare->fetchAll();
echo $num; //OUTPUT 3
echo count($result1); //OUTPUT 2
if($num > 0){
foreach ($result1 as $x => $row) {
//LOOPING only 2 times, 1 row is not showing
}
}
The fetchAll() function is only returning 2 rows. How come?
Upvotes: 0
Views: 2286
Reputation: 157893
Your code contradicts with your words. most likely you are calling fetchColumn before fetchAll - so, fetchColumn snatches one row from the resultset, leaving only two for fetchAll.
Anyway, you need none of these
$stm = $DB->prepare("SELECT * table");
$stm->execute();
$data = $stm->fetchAll();
foreach ($data as $x => $row) {
//LOOPING only if there was data returned. no need to check the number
}
Upvotes: 5