php - 如何使我的导出文件导出更大的表

标签 php mysql

我的导出文件只能导出有限数量的数据。这就是为什么我只导出从事件中的 uuid 大于或等于 772345 开始的行。但是表中还有很多数据。有谁知道能够导出整个表吗?

这是我正在使用的导出文件:

    <?php
$db_con = mysqli_connect("localhost", "root", "", "monitoring");
$result = $db_con->query('SELECT * FROM event where uuid >= 772345 and host != "" ');
if (!$result) die('Couldn\'t fetch records');
$num_fields = mysqli_num_fields($result);
$headers = array();
while ($fieldinfo = mysqli_fetch_field($result)) {
    $headers[] = $fieldinfo->name;
}
$fp = fopen('php://output', 'w');
if ($fp && $result) {
    header('Content-Type: text/csv'); 
    header('Content-Disposition: attachment; filename="event.csv"');
    header('Pragma: no-cache');
    header('Expires: 0');
    fputcsv($fp, $headers);
    while ($row = $result->fetch_array(MYSQLI_NUM)) {
        fputcsv($fp, array_values($row));
    }
    die;
}
?>    

这是我尝试导出全部内容时遇到的错误: fatal error :第 3 行 C:\xampp\htdocs\exports\export_event_all.php 中允许的内存大小 134217728 字节已耗尽(尝试分配 1146880 字节)

最佳答案

另一个解决方案是告诉 mysqli_ 扩展不要缓冲查询结果。默认情况下,查询结果返回给 PHP,所有行都存储在 PHP 内存中的数组中。

您可以使用上的参数关闭此模式

$db_con->query('SELECT * FROM event where host != ""', MYSQLI_USE_RESULT);

http://php.net/manual/en/mysqlinfo.concepts.buffering.php

From the manual

Queries are using the buffered mode by default. This means that query results are immediately transferred from the MySQL Server to PHP and then are kept in the memory of the PHP process. This allows additional operations like counting the number of rows, and moving (seeking) the current result pointer. It also allows issuing further queries on the same connection while working on the result set. The downside of the buffered mode is that larger result sets might require quite a lot memory. The memory will be kept occupied till all references to the result set are unset or the result set was explicitly freed, which will automatically happen during request end the latest. The terminology "store result" is also used for buffered mode, as the whole result set is stored at once.

Unbuffered MySQL queries execute the query and then return a resource while the data is still waiting on the MySQL server for being fetched. This uses less memory on the PHP-side, but can increase the load on the server. Unless the full result set was fetched from the server no further queries can be sent over the same connection. Unbuffered queries can also be referred to as "use result".

Following these characteristics buffered queries should be used in cases where you expect only a limited result set or need to know the amount of returned rows before reading all rows. Unbuffered mode should be used when you expect larger results.

因此,如果您像这样更改query(),它将起作用

<?php
$db_con = mysqli_connect("localhost", "root", "", "monitoring");
$result = $db_con->query('SELECT * FROM event where host != ""', MYSQLI_USE_RESULT);
//  the change                                                   ^^^^^^^^^^^^^^^^
if (!$result) die('Couldn\'t fetch records');
$num_fields = $result->field_count;
$headers = array();
while ($fieldinfo = mysqli_fetch_field($result)) {
    $headers[] = $fieldinfo->name;
}
$fp = fopen('php://output', 'w');
if ($fp && $result) {
    header('Content-Type: text/csv'); 
    header('Content-Disposition: attachment; filename="event.csv"');
    header('Pragma: no-cache');
    header('Expires: 0');
    fputcsv($fp, $headers);
    while ($row = $result->fetch_array(MYSQLI_NUM)) {
        fputcsv($fp, array_values($row));
    }
    die;
}
?>    

关于php - 如何使我的导出文件导出更大的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43542947/

相关文章:

php - CakePHP 的模型关联

php - Paypal :从沙箱上线

php - JavaScript 标签在 HTML 代码中生成中断

PHPSpreadsheet - 如何将链接中的图像放入我的 excel 文件中?

php - 如何循环获取过去十二个月开始和结束的 unix 时间?

php oop 和 mysql 查询

mysql - CodeIgniter 事件记录;将两个查询合并为一个?

php - 类 Closure 的对象无法转换为 : filename. 中的字符串

java - 如何在 php web 脚本中获取 java 应用程序的输出?

mysql 将我的域名附加到我的用户名中