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 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据