Problem Dynamic Excel download Mysql

Aug 15, 2012 at 7:11 PM


I'll using following code (modified for my data/database) from your webpage 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'); ?>

/** 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)
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);

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

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');