Problem Dynamic Excel download Mysql

Topics: Developer Forum, User Forum
Aug 15, 2012 at 6:33 PM

Hello,

I'll using following code (modified for my data/database) from your webpage http://phpexcel.codeplex.com/workitem/17275 but with this code I'll get following error message:

Notice: Undefined variable: db in Z:\xampp\htdocs\labelmanagementDB\Test_Excel1.php on line 13
Fatal error: Call to a member function query() on a non-object in Z:\xampp\htdocs\labelmanagementDB\Test_Excel1.php on line 13


Please help me to solve the problem...

Below my modified code:

<!--Connection to the Database-->
<?php require_once('Connections/Connection.php'); ?>

<?php
/** PHPExcel */
require_once 'Classes/PHPExcel.php';

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Database with existing object
$sql = "SELECT * FROM `tbltechnical` ORDER BY `ID` ASC";
$res = $db->query($sql);

// First Row (Names)
$objPHPExcel->setActiveSheetIndex(0);
foreach ($res->getColumnNames() as $coluna=>$num) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow ($num,1,$coluna);
}

// Other Rows (Data)
$i = 2;
while ($row = $res->fetchrow()) {
foreach ($row as $col=>$dado) $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow ($col,$i,$dado);
$i++;
}

// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="table.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
?>
Coordinator
Aug 15, 2012 at 11:17 PM

Nothing to do with PHPExcel, but it looks as though your database connection is failing so $db is undefined