gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:13:25 24 4
gpt4 key购买 nike

我正在尝试迭代结果集并获取列名称和值,以便使用 PHPExcel 导出数据,但是 $qVistadet->fetchall 在下面的代码中不起作用,并且收到诸如 undefined offset 之类的错误。

但是$qVistadet->fetch可以工作,但由于它只返回结果集中的第一行,所以没有用。

有人可以帮忙解决这个问题吗?

    $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++;
}

最佳答案

以下更改解决了该问题。谢谢。

$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;
}

关于php - fetchall (PDO) 结果集迭代不返回 $key=>$value 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36168610/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com