douhuang2218 2016-03-23 02:09
浏览 53

fetchall(PDO)结果集迭代不返回$ key => $ value的值

I am trying to iterate through the resultset and get the column names and the values,for exporting the data using PHPExcel, but the $qVistadet->fetchall is not working in the below code, and gets errors like undefined offset.

But$qVistadet->fetch, works but as it only returns the first row in the resultset, it is not useful.

Could anyone help on the issue?

    $qVistadet = $db->prepare("CALL spvistadetailsbystaffid(?)");
    $qdetails->bindParam (1, $staffid);
    $qdetails->execute();

        $col=0;
        $row1=1;

        while($row = $qVistadet->fetchAll(PDO::FETCH_ASSOC))
           {
        foreach ($row as $key=>$value)
              {         
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $key);
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row1 + 1, $value);
                $col++;
              }
            $row1++;
           }
  • 写回答

1条回答 默认 最新

  • dongyan1936 2016-03-23 06:18
    关注

    The below change fixed the issue. Thanks.

    $qVistadet = $db->prepare( "CALL spvistadetailsbystaffid(?)" );
    $qVistadet->bindParam ( 1, $staffid );
    $qVistadet->execute();
    
    
    $col=0;
    $row1=2;
    while ($arrValues = $qVistadet->fetch(PDO::FETCH_ASSOC))
        {
    
            foreach ($arrValues as $key=>$value){
    
                $sheet->setCellValueByColumnAndRow($col, 1, $key);
                $sheet->setCellValueByColumnAndRow($col, $row1, $value);
                $col++;
                }
            $row1++;
            $col=0;
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看