0

我有一个包含 3 行的表。我正在尝试遍历所有行,但没有得到正确数量的行。

我的代码如下:

$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

    }


}

fetchAll() 函数只返回 2 行。怎么会?

4

1 回答 1

5

你的代码与你的话相矛盾。您很可能在 fetchAll 之前调用 fetchColumn - 因此,fetchColumn 从结果集中抢夺一行,只为 fetchAll 留下两行。

无论如何,你不需要这些

$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

}
于 2013-05-03T16:19:07.560 回答