I have a multi-sheet xlsx I'm creating using PHPSpreadsheet. The SQL query works great on the first page, but only the headers return on the other worksheets. I've tried separate SQL queries for each sheet and I've tried separate while statements for each sheet, but no matter what, the sheets after the 1st will not return query results. Here's a sample of code:
$sql = "SELECT Field1, Field2, Facility FROM dbo.table WHERE date
BETWEEN '$from' AND '$to'";
$query = sqlsrv_query( $conn, $sql );
$row = 2;
$spreadsheet->setActiveSheetIndex(0);
while( $data = sqlsrv_fetch_object( $query )) {
if ($data->Facility=='Facility1'){
$spreadsheet->getActiveSheet()
->setCellValue('A'.$row , $data->Facility)
->setCellValue('B'.$row , $data->Field1)
->setCellValue('C'.$row , $data->Field2);
$row++; } }
$spreadsheet->getActiveSheet()
->setCellValue('A1' , 'Facility Type')
->setCellValue('B1' , 'Facility ID')
->setCellValue('C1' , 'Location');
// Sheet 2
$spreadsheet->createSheet();
$spreadsheet->setActiveSheetIndex(1);
while( $data = sqlsrv_fetch_object( $query )) {
if ($data->Facility=='Facility2'){
$spreadsheet->getActiveSheet()
->setCellValue('A'.$row , $data->Facility)
->setCellValue('B'.$row , $data->Field1);
Why would it work for the first page, but not pages 2-7?