douju7503 2013-08-25 21:04 采纳率: 0%
浏览 33
已采纳

PDO获取循环永远不会结束

I have a loop that never ends and its only pulling one record from the database.

I have a class that returns a list then I call that class function on a website where the while loop never stops echoing the data.

class exampleclass
{
public function get_list()
{
$query = $dbh->prepare("SELECT .. blah);
$query->bindParam("blah");
$query->execute();
return $query->fetch(PDO::FetchObj);
}

on the web page i have:

<?php
testobj = new exampleclass();
while ($obj = $testobj->get_list())
{
echo $obj->db_field;
}
?>

It pulls the one record from the database but it doesn't stop looping, it should just loop once.

  • 写回答

1条回答 默认 最新

  • douwei7501 2013-08-25 21:12
    关注

    First of all, quit that idea of creating objects out of DB rows. They look fancy yet totally useless. Then learn how to return whole result and how to properly iterate over it:

    public function get_list()
    {
        $stmt = $dbh->prepare("SELECT .. blah");
        $query->execute(array("blah"));
        return $query->fetchAll();
    }
    
    testobj = new exampleclass();
    foreach ($testobj->get_list() as $row)
    {
        echo $row['db_field'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示