I am using below code to export columns from two mysql table to an excel file using phpexcel.
I am achieving it by running the query twice. Can someone help me on how to get this done by running only one query and using LIKE and UNION maybe.
Thank You!
EDIT: As per requirement, the number of table will increase going forward but will have a same kind of name...So have to find a solution which will select all the tables on the database using LIKE first and then query the column data from all the tables. And then output those data to an excel sheet side-by-side. Like table 1 data on col A1-AJ, the tab;e 2 data from AL-AR and so on.
Here is my code:
<?php
// connection with the database
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "redhat";
$dbname = "was";
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);
// require the PHPExcel file
require 'phpexcel/Classes/PHPExcel.php';
$query = "SELECT servicedate,recdate,dostoreclag,casesrec,scandate,casesentered,casespending,casecountdiff,entrydate,rcvdtolag FROM hos1report";
$query1 = "SELECT servicedate,recdate,dostoreclag,casesrec,scandate,casesentered,casespending,casecountdiff,entrydate,rcvdtolag FROM hos2report";
$headings = array('Service Date','Rec. Date','DOS to Rec. Lag','Cases Rec.','scan Date','Cases Entered','Cases Pending','Cases Count Diff','Entry Date','Rec. to Entry Lag');
if ($result = mysql_query($query) or die(mysql_error())) {
// Create a new PHPExcel object
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('Report');
$rowNumber = 8;
$col = 'A';
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}
// Loop through the result set
$rowNumber = 9;
while ($row = mysql_fetch_row($result)) {
$col = 'A';
foreach($row as $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
}
} else {
echo 'a problem has occurred... no data retrieved from the database';
}
if ($result1 = mysql_query($query1) or die(mysql_error())) {
// Create a new PHPExcel object
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet();
$rowNumber = 8;
$col = 'L';
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}
// Loop through the result set
$rowNumber = 9;
while ($row = mysql_fetch_row($result)) {
$col = 'L';
foreach($row as $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
}
} else {
echo 'a problem has occurred... no data retrieved from the database';
}
// Freeze pane so that the heading line won't scroll
$objPHPExcel->getActiveSheet()->freezePane('A2');
// Save as an Excel BIFF (xls) file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="report.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
?>
All the tables will have same columns but different data
