user3120521
user3120521

Reputation: 141

phpexcel export from database missing first row of data

I am trying to export some data from my MySQL database to an excel spreadsheet. When I run the query I get 3 rows of data. When I try to use phpexcel to create the excel spreadsheet only the last 2 records get printed.

I have found similar posts, but none of the answers have worked for me yet. Thanks for any help.

here is the segment of code I am working with for the loop:

if ($result->fetch_assoc()) { 
    // Create a new PHPExcel object 
    $objPHPExcel = new PHPExcel(); 
    $objPHPExcel->getActiveSheet()->setTitle('filename'); 

    $rowNumber = 1; 
    $col = 'A'; 
    foreach($headings as $heading) { 
       $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading); 
       $col++; 
    } 

    // Loop through the result set 
    $rowNumber++;
    while ($row = $result->fetch_assoc()) { 
       $col = 'A'; 
       foreach($row as $cell) { 
          $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell); 
          $col++; 
       } 
       $rowNumber++; 
    } 

Upvotes: 0

Views: 732

Answers (1)

ChrisCool
ChrisCool

Reputation: 134

The first line is wrong : if ($result->fetch_assoc()) { Indeed doing that you fetch the first row but you don't use it and after you iterate on the next rows.

replace it by : if ($result) {

Upvotes: 1

Related Questions