【问题标题】:php export MySql to excel sheet wont workphp 将 MySql 导出到 excel 表不起作用
【发布时间】:2015-06-26 16:04:30
【问题描述】:

我收到的错误消息是标题。我正在使用 mysqli_ 我在空白页上使用 mysql_ 来测试它。当我把它放到我真实页面上的点击中时,它告诉我我不能使用 mySql_,因为它太旧了,所以将它转换为 mySqli_。我现在收到的错误消息是:

“警告:无法修改标头信息 - 标头已由 (输出开始于 /xx/xxx/xxx.php:183)在 /xxx/xxx/xxx.php 在线 232 警告:无法修改标头信息 - 标头已发送 通过(输出开始于 /xxx/xxx/xxx.php:183)在 /xxx/xxx/xxx.php 上 第 233 行致命错误:调用未定义的函数 outputcsv() /xxx/xxx/xxx.php 在第 237 行"

require_once 'dbconfig.php';

$conn = new mysqli("xxxx", "xxxx", $password, $dbname);//host, user, password, database

$result = $conn->query('SELECT * FROM reports') or die(mysqli_error());

//these two lines are the lines 232 and 233
header('Content-Type: text/csv'); // tell the browser to treat file as CSV
header('Content-Disposition: attachment;filename=report.csv'); // tell browser to download a file in user's system with name export.csv

$row = mysqli_fetch_assoc($result); // Get the column names
if ($row) {
    outputcsv(array_keys($row)); // It wil pass column names to outputcsv function
}

//this line here is 237
while ($row) {
    outputcsv($row);    // loop is used to fetch all the rows from table and pass them to outputcsv func
    $row = mysqli_fetch_assoc($result);
 }

 function outputcsv($fields) {
   $csv = '';
    foreach ($fields as $field) {
        $csv .= '"' . $field . '",';
    }
    $csv .= "\r\n";     //Give a carriage return and new line space after each record
    echo $csv;
}

【问题讨论】:

标签: php mysql excel csv


【解决方案1】:

你需要在配置文件中设置输出缓冲区

只需在第一行代码写 ob_start() ..

我还建议以 xls 格式导出以获得更好的兼容性。

【讨论】:

    【解决方案2】:

    您需要查找php output buffering

    您在第 183 行 headers already sent by (output started at /xx/xxx/xxx.php:183) 上回显或发送某种数据,然后修改标题。您可以使用 OB 来缓冲要发送到浏览器的信息,修改标头,然后刷新缓冲区。

    ob_start();
    require_once 'dbconfig.php';
    
    $conn = new mysqli("xxxx", "xxxx", $password, $dbname);//host, user, password, database
    
    $result = $conn->query('SELECT * FROM reports') or die(mysqli_error());
    
    //these two lines are the lines 232 and 233
    header('Content-Type: text/csv'); // tell the browser to treat file as CSV
    header('Content-Disposition: attachment;filename=report.csv'); // tell browser to download a file in user's system with name export.csv
    
    $row = mysqli_fetch_assoc($result); // Get the column names
    if ($row) {
        outputcsv(array_keys($row)); // It wil pass column names to outputcsv function
    }
    
    //this line here is 237
    while ($row) {
        outputcsv($row);    // loop is used to fetch all the rows from table and pass them to outputcsv func
        $row = mysqli_fetch_assoc($result);
     }
    
     function outputcsv($fields) {
       $csv = '';
        foreach ($fields as $field) {
            $csv .= '"' . $field . '",';
        }
        $csv .= "\r\n";     //Give a carriage return and new line space after each record
        echo $csv;
    }
    ob_flush();
    

    您也可以使用ob_get_flush()ob_get_contents() 将缓冲区的内容返回为字符串,从而允许从函数等中返回。注意:ob_get_contents() 不会清除缓冲区,它只是返回内容点。

    我还看到关于未定义 outputcsv($fields) 的错误,但在解释器可以解决标头问题后,这可能会清除。

    【讨论】:

      猜你喜欢
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多