我正在尝试遍历结果集并获取列名和值,以便使用 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;
}