PHP 无法将大数据写入 csv 文件
PHP can't write big data to csv file
我正在使用 PHP 代码将数据从 table(数据库)导出到 CSV 文件。
但不幸的是,它只写了 98000 行,而我在 table 中总共有 198000 行。
我认为 table 中的总数据高达 34mb。
第二件事,一旦文件写入完成,我如何自动下载。任何人都可以帮助谢谢。
$que = "SELECT * FROM campaign";
$query = mysqli_query($connection,$que);
$number_rows = mysqli_num_rows($query);
if ($number_rows >= 1)
{
$filename = "exported_db_" . date("m-d-Y_hia") . ".csv"; // filenme with date appended
$fp = fopen($filename, "w"); // open file
$row = mysqli_fetch_assoc($query);
$seperator = "";
$comma = "";
foreach ($row as $name => $value)
{
$seperator .= $comma . $name; // write first value without a comma
$comma = ","; // add comma in front of each following value
}
$seperator .= "\n";
echo "Database has been exported to $filename";
fputs($fp, $seperator);
mysqli_data_seek($query, 0); // use previous query leaving out first row
while($row = mysqli_fetch_assoc($query))
{
$seperator = "";
$comma = "";
foreach ($row as $name => $value)
{
$seperator .= $comma . $value; // write first value without a comma
$comma = ","; // add comma in front of each following value
}
$seperator .= "\n";
fputs($fp, $seperator);
}
fclose($fp);
}
else {
echo "There are no records in the database to export.";
}
当我将这两行代码放在文件的顶部时,我得到了正确的结果。
我认为这些代码会增加 caches_memory 的大小和执行时间。
感谢大家的评论和讨论。
ini_set('memory_limit', '512M');
ini_set('max_execution_time', '180');
我正在使用 PHP 代码将数据从 table(数据库)导出到 CSV 文件。
但不幸的是,它只写了 98000 行,而我在 table 中总共有 198000 行。
我认为 table 中的总数据高达 34mb。
第二件事,一旦文件写入完成,我如何自动下载。任何人都可以帮助谢谢。
$que = "SELECT * FROM campaign";
$query = mysqli_query($connection,$que);
$number_rows = mysqli_num_rows($query);
if ($number_rows >= 1)
{
$filename = "exported_db_" . date("m-d-Y_hia") . ".csv"; // filenme with date appended
$fp = fopen($filename, "w"); // open file
$row = mysqli_fetch_assoc($query);
$seperator = "";
$comma = "";
foreach ($row as $name => $value)
{
$seperator .= $comma . $name; // write first value without a comma
$comma = ","; // add comma in front of each following value
}
$seperator .= "\n";
echo "Database has been exported to $filename";
fputs($fp, $seperator);
mysqli_data_seek($query, 0); // use previous query leaving out first row
while($row = mysqli_fetch_assoc($query))
{
$seperator = "";
$comma = "";
foreach ($row as $name => $value)
{
$seperator .= $comma . $value; // write first value without a comma
$comma = ","; // add comma in front of each following value
}
$seperator .= "\n";
fputs($fp, $seperator);
}
fclose($fp);
}
else {
echo "There are no records in the database to export.";
}
当我将这两行代码放在文件的顶部时,我得到了正确的结果。
我认为这些代码会增加 caches_memory 的大小和执行时间。
感谢大家的评论和讨论。
ini_set('memory_limit', '512M');
ini_set('max_execution_time', '180');