我正在运行 MySQL 的 PHPExcel 输出。
当我运行代码并使用几行输出输出到 Excel 时,一切都很好,当我运行以下代码时,根据 zend 服务器日志,除了非常长的查询执行时间之外,我什么也没得到。
我根本没有得到输出,我在似乎永恒之后收到了打开 PHP 文件的请求!
感谢任何帮助。
这是到目前为止的代码:
$queryGetIMEI = "SELECT DISTINCT(wi_iridium_og_device) FROM wi_iridium_og ORDER BY wi_iridium_og_device DESC";
//declare the new array for the IMEI numbers
$IMEI = array();
if ($result = $mysqli->query($queryGetIMEI)) {
/* fetch associative array */
while ($row = $result->fetch_array()) {
$IMEI[] = $row["wi_iridium_og_device"];
}
/* free result set */
$result->free();
}
// call the PHPexcel Files
require_once 'classes/PHPExcel.php';
require_once 'classes/PHPExcel/IOFactory.php';
$objPHPExcel = new PHPExcel();
$sheet_count = 0;
foreach ($IMEI as $c) {
if ($sheet_count > 0) {
// This creates the next sheet in the sequence
// One sheet per IMEI in this example
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex($sheet_count);
}
// Add tab label to the sheet
$objPHPExcel->getActiveSheet()->setTitle(substr($c,0,30));
// Column headings in the first row
$objPHPExcel->getActiveSheet()->setCellValue('A1','Device');
$objPHPExcel->getActiveSheet()->setCellValue('B1','Site Reference');
$objPHPExcel->getActiveSheet()->setCellValue('C1','Charge Type');
$objPHPExcel->getActiveSheet()->setCellValue('D1','Date');
$objPHPExcel->getActiveSheet()->setCellValue('E1','Time');
$objPHPExcel->getActiveSheet()->setCellValue('F1','Number Called');
$objPHPExcel->getActiveSheet()->setCellValue('G1','Service');
$objPHPExcel->getActiveSheet()->setCellValue('H1','Call Termination');
$objPHPExcel->getActiveSheet()->setCellValue('I1','MSISDN');
$objPHPExcel->getActiveSheet()->setCellValue('J1','Originating Country');
$objPHPExcel->getActiveSheet()->setCellValue('K1','UNUSED_3');
$objPHPExcel->getActiveSheet()->setCellValue('L1','UNUSED_4');
$objPHPExcel->getActiveSheet()->setCellValue('M1','UNUSED_5');
$objPHPExcel->getActiveSheet()->setCellValue('N1','UNUSED_6');
$objPHPExcel->getActiveSheet()->setCellValue('O1','UNUSED_7');
$objPHPExcel->getActiveSheet()->setCellValue('P1','UNUSED_8');
$objPHPExcel->getActiveSheet()->setCellValue('Q1','Units');
$objPHPExcel->getActiveSheet()->setCellValue('R1','Currency');
$objPHPExcel->getActiveSheet()->setCellValue('S1','Charge');
// Dynamic data comes next
// Query the DB based on the IMEI value. Pass the result to the while loop
$query_GetBillPerIMEI = "SELECT SQL_NO_CACHE wi_iridium_og_device, wi_iridium_og_charge_type, wi_iridium_og_date, wi_iridium_og_time, wi_iridium_og_number_called, wi_iridium_og_service FROM wi_iridium_og WHERE wi_iridium_og_device = ". $c ." AND wi_iridium_og.wi_iridium_og_charge_type != 'SBD Overage' ORDER BY wi_iridium_og_charge_type ASC";
$result = $mysqli->query($query_GetBillPerIMEI);
$rowcount = 2;
while ($row = $result->fetch_array()){
$objPHPExcel->getActiveSheet()->SetCellValue('A' .$rowcount, $row['wi_iridium_og_device']);
$objPHPExcel->getActiveSheet()->SetCellValue('B' .$rowcount, $row['wi_iridium_og_site_reference']);
$objPHPExcel->getActiveSheet()->SetCellValue('C' .$rowcount, $row['wi_iridium_og_charge_type']);
$objPHPExcel->getActiveSheet()->SetCellValue('D' .$rowcount, $row['wi_iridium_og_date']);
$objPHPExcel->getActiveSheet()->SetCellValue('E' .$rowcount, $row['wi_iridium_og_time']);
$objPHPExcel->getActiveSheet()->SetCellValue('F' .$rowcount, $row['wi_iridium_og_number_called']);
$objPHPExcel->getActiveSheet()->SetCellValue('G' .$rowcount, $row['wi_iridium_og_service']);
当我将以下行添加到输出时:
$objPHPExcel->getActiveSheet()->SetCellValue('G' .$rowcount, $row['wi_iridium_og_service']);
该文件无法运行并产生输出。
我的数据库中有超过 30,000 个条目需要查询。
救命!!!
最佳答案
首先阅读 PHP PDO和 Prepared Statements :这足以处理/避免 SQL 注入(inject)(您查询的问题在这里 WHERE wi_iridium_og_device = ". $c ."AND
因为您不知道 的值$c
这可能是有害的)。
关于如何返工以提高源代码性能...(这不会起作用,将其视为伪代码)
<?php
// call the PHPexcel Files
require_once 'classes/PHPExcel.php';
require_once 'classes/PHPExcel/IOFactory.php';
$objPHPExcel = new PHPExcel();
$sheet_count = 0;
$queryGetIMEI = "SELECT DISTINCT(wi_iridium_og_device) FROM wi_iridium_og ORDER BY wi_iridium_og_device DESC";
//declare the new array for the IMEI numbers
if ($result = $mysqli->query($queryGetIMEI))
{
/* fetch associative array */
while ($row = $result->fetch_array())
{
// your foreach login in here (without the foreach)
}
}
这应该会改善您的内存占用量,但您也可能能够改善查询。
关于php - MySQL PHPExcel 查询执行时间太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25623480/