我从我的数据库中循环行,除了一件事外,它行得通。跳过第一个ID。
它从第二条记录开始。任何想法如何解决这个问题?
这是我的代码:

<?php
$query = $PDO->prepare('SELECT * FROM pokes');
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC)
?>
<?php
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$id = $row['id'];
$n = $row['name'];
$cp = $row['cp'];
echo $id . ' ' . $n . ' ' . $cp . '<br>';
}
?>

最佳答案

<?php
 // your first error is here. You are fetching the first row
 $row = $query->fetch(PDO::FETCH_ASSOC)
 // And here you start from the second, since you already did ones above
 while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
  //...rest of oyur code
 }
?>


你有两种方法来完成你的任务

  <?php
  // Just add the PDO::FETCH_ASSOC constant while you are looping
  while($row = $query->fetch(PDO::FETCH_ASSOC)){
   //...Code here
  }

  // another way is adding the constant before using it
  $query->setFetchMode(PDO::FETCH_ASSOC);
  while($row = $query->fetch()){
   //...Code here
  }
  ?>

07-24 09:50
查看更多