Sainath Krishnan
Sainath Krishnan

Reputation: 2139

Exporting to Excel with PHPExcel and MySQL

I am trying to export some data to an Excel sheet using PHPExcel. However, it leaves me with a blank page and nothing else (I understand that this is normal, however the download does not trigger either).

EDIT : Opening a new question for the rest of this problem. Thanks for the help on this all!

Am I doing something wrong here? This is my code (updated as per above)

Problem : SOLVED. Error : Mismatch in variable name ($RowCount vs $Rowcount).

<?php
$download="";
if (isset($_GET['surveyid'])) {
//Survey ID
$download = $_GET['surveyid'];
require_once('../Classes/PHPExcel.php');

$query=sprintf("SELECT b.question_id as qid, a.question as ques, b.response as response, count(b.response) as count
FROM v3_sai.survey_responses b 
INNER JOIN v3_sai.survey_questions a ON a.id = b.question_id AND a.survey_id=%d
group by b.response, a.question
order by b.question_id;",GetSQLValueString($download,"int"));
$resultdl= mysql_query($query) or die(mysql_error());
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$rowcount=1;
while($row = mysql_fetch_array($resultdl)){
     $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowcount, $row['qid']);
     $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowcount, $row['ques']); 
     $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowcount, $row['response']);
     $objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowcount, $row['count']); 
$rowCount++; 
} 
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="file.xls"');
$objWriter->save('php://output');

}

Upvotes: 1

Views: 9881

Answers (2)

voodoo417
voodoo417

Reputation: 12101

You did some mistake in your SQL query:

"SELECT b.question_id as qid, a.question as ques, b.response as response,  count(b.response) as count ..."

See on count(b.response) as count. Change alias count to another.

Also remove ; in end of query.

EDIT

Function GetSQLValueString is not defined and not native. Change code to:

$query = "SELECT b.question_id as qid,
                a.question as ques,
                b.response as response,
                count(b.response) as cnt
          FROM v3_sai.survey_responses b 
          INNER JOIN v3_sai.survey_questions a 
             ON a.id = b.question_id 
                AND a.survey_id=".intval($download)."
          group by b.response, a.question
          order by b.question_id";

Also change this line:

 $objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount, $row['cnt']); 

Upvotes: 2

Joey Novak
Joey Novak

Reputation: 173

Blank pages usually mean display_errors is off... Which makes debugging a pain.

Set, or add the following to your php.ini, and restart the server (p.s. if you have multiple php.ini files, use the phpinfo() function to make sure you are editing the right one).

display_errors = On

I also turn on error logging so I can find errors and fix them after the fact, so these php.ini file edits will enable it:

log_errors = On
error_log = n:/uniservers/coral/php_errors.log (set your destination here).

Once you have display errors turned on, it should be a piece of cake for you to find the error.

Joey

Upvotes: 1

Related Questions