我有一个代码,使用php从mysql获取数据并将其导出到excel文件,其中:
<?php
$xls_filename = "export_".rand(100000,999999).".xls"; // Define Excel (.xls) file name
// Header info settings
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$xls_filename");
header("Pragma: no-cache");
header("Expires: 0");
/***** Start of Formatting for Excel *****/
// Define separator (defines columns in excel & tabs in word)
$sep = "\t"; // tabbed character
$slct2 = $dbh->prepare("SELECT * FROM forms_answers WHERE ans_formid='{$identity}'");
$slct2->execute();
while($name = $slct2->fetch(PDO::FETCH_ASSOC)) {
$stmt2 = $dbh->prepare("SELECT * FROM forms_answers_details WHERE ans_id='{$name['ans_id']}'");
$stmt2->execute();
// Start of printing column names as names of MySQL fields
for($i=0; $i<$stmt2->columnCount(); $i++) {
echo $stmt2->getColumnMeta($i)."\t";
}
print("\n");
// End of printing column names
// Start while loop to get data
while($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
$schema_insert = "";
for($j=0; $j<$stmt2->columnCount(); $j++) {
if(!isset($row[$j])) {
$schema_insert .= "NULL".$sep;
}
elseif(isset($row[$j])) {
$schema_insert .= "$row[$j]".$sep;
}
else {
$schema_insert .= "".$sep;
}
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
}
}
?>
但问题是这个代码只是将数据导出到一个扩展,假设它几乎导出一个8 * 20 excel表,并且不能导出超过该限制的数据。相反,它导出数组,NULL和页面代码。任何人都可以建议我该怎么做才能解决这个问题?
答案 0 :(得分:1)
$ slct2 = $ dbh-&gt; prepare(“SELECT * FROM forms_answers WHERE ans_formid ='{$ identity}'LIMIT 1000”);
$ stmt2 = $ dbh-&gt; prepare(“SELECT * FROM forms_answers_details WHERE ans_id ='{$ name ['ans_id']}'LIMIT 1000”);
尝试使用它可能是Db服务器设置限制的可能性。
根据数据更改限制
否则,由于内存和执行时间不足,其进程可能会在中间中断。
尝试使用这两行
的ini_set( '的max_execution_time',300);
函数ini_set( 'memory_limit的', '512M')
将这两行放在取数数据之前;
它会帮助你。