Query displaying less result than actually exists in export

3

I have set a query with a list resulting from a selection of checkbox made by the user, the list is with the correct values, as selected, but when checking the content of the result after query in banco de dados , it is always me displayed one less record.

My list is being set up like this:

// LISTA
$colname_rcCandidato = "-1";
if (isset($_GET['list'])) {
  $colname_rcCandidato = $_GET['list'];
}

My search is thus using the list

mysql_select_db($database_pcon, $pcon);
$query_rcLista = sprintf("SELECT   ongAtleta.IdAtleta, 
                                       ongAtleta.Nome, 
                                       ongEscola.Nome AS NomeEscola, 
                                       ongEscola.Municipio,
                                       ongAtleta.ProfResposavel
                                 FROM  ongAtleta
                                 INNER JOIN ongFichaEscolar ON ( ongAtleta.IdAtleta = ongFichaEscolar.IdAtleta ) 
                                 INNER JOIN ongEscola ON ( ongFichaEscolar.IdEscola = ongEscola.IdEscola ) 
                                 WHERE ongAtleta.IdAtleta IN (%s)", GetSQLValueString($colname_rcCandidato, "string"));
$rcLista = mysql_query($query_rcLista, $pcon) or die(mysql_error());
$row_rcLista = mysql_fetch_assoc($rcLista);
$linhas = mysql_num_rows($rcLista);

My export is like this, I'm using classe PHPExcel :

if ($linhas > 0) {

    // Incluimos a classe PHPExcel
    include  '../_comp/externos/classes/PHPExcel.php';

    // Instanciamos a classe
    $objPHPExcel = new PHPExcel();

    // Inicializando variável
    $rowCount = 1; 

    do {

        $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, removeAcentos($row_rcLista['Nome'])); 
        $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, removeAcentos($row_rcLista['NomeEscola'])); 
        $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, removeAcentos($row_rcLista['ProfResposavel'])); 
        $objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount, removeAcentos($row_rcLista['Municipio'])); 

        // Incrementando variável
        $rowCount++;    

    } while($row_rcLista = mysql_fetch_assoc($rcLista));

    // Nome da Planilha
    $objPHPExcel->getActiveSheet()->setTitle('AlunoEscola');

    // Efetua Download
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="RelacaoAlunoEscola.xls"');
    header('Cache-Control: max-age=0');
    // Se for o IE9, isso talvez seja necessário
    header('Cache-Control: max-age=1');

    // Salvando o Arquivo
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

    // Salva diretamente no output
    $objWriter->save('php://output'); 

    exit;

}

In the example I am posting to an image, 4 records are selected and only 3 are displayed in the export.

    
asked by anonymous 20.01.2016 / 14:58

1 answer

2

Based strictly on the details of the question, the problem seems to be do-while , $row_rcLista will only have a value after the mysql_fetch_assoc() call that is made at the end or a blank line is added. Change the do-while to a while simple.

do {
    $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, removeAcentos($row_rcLista['Nome']));
    //código omitido 

    $rowCount++;    

} while($row_rcLista = mysql_fetch_assoc($rcLista)); // <---- atribuição

Solution:

while($row_rcLista = mysql_fetch_assoc($rcLista){
   $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, removeAcentos($row_rcLista['Nome'])); 
   //código omitido
   $rowCount++;  
}
    
20.01.2016 / 15:35