Query displaying less result than actually exists in export

Asked

Viewed 90 times

4

I rode 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 consultation on banco de dados I am always shown a record less.

My list is being put together like this:

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

My research 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 the 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’m posting on an image, 4 records are selected and only 3 are displayed on the export.

inserir a descrição da imagem aqui

  • Could be a cell formatting error, from a f2 in it and see if there’s anything.

1 answer

2


Based rigidly on the details of the question the problem seems to be the do-while, $row_rcLista will only have a value after the call of mysql_fetch_assoc() which is done at the end or a blank line is added. Change the do-while for a whilesimple.

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++;  
}
  • Hello @rray, thank you so much for the excellent tip, I solved the problem.

  • @adventistapr what was the real problem?

  • Hello @rray, sorry it took so long to reply, the answer is exactly what you posted: "$row_rcList will only have a value after the call of mysql_fetch_assoc()"

  • @good legal adventistapr that solved :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.