【问题标题】:Problems creating a .csv download创建 .csv 下载的问题
【发布时间】:2013-02-14 08:23:07
【问题描述】:

我正在尝试从 mysql 表创建 .csv 下载,但没有取得多大成功。我已经从我在这里找到的教程http://code.stephenmorley.org/php/creating-downloadable-csv-files/ 中汇总了下面的代码,但由于某种原因,下载的 .csv 文件包含页面的整个源代码,而不是 mysql 表中的列标题和数据。

任何帮助将不胜感激:)

    // output headers so that the file is downloaded rather than displayed
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=site-list-export.csv');

    // create a file pointer connected to the output stream
    $output = fopen('php://output', 'w');

    // output the column headings
    fputcsv($output, array('id', 'url', 'clean_host', 'keyword', 'group', 'page_number', 'page_authority', 'domain_authority', 'page_mozrank', 'seomoz', 'page_title', 'check_inbound', 'check_inbound_priority ', 'count', 'project', 'type'));


    // fetch the data
    $connect = mysql_connect("localhost", DB_USERNAME, DB_PASSWORD);
    mysql_select_db(DB_DATABASE, $connect);

    $rows = mysql_query("SELECT * 
    FROM `url_list_google`
    WHERE `group` = '".$selected_group."' 
    GROUP BY clean_host 
    ORDER BY `domain_authority` DESC") or die(mysql_error());
    mysql_close($connect);

    // loop over the rows, outputting them
    while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);

    }

【问题讨论】:

  • 您可以随时安装 phpMyAdmin,因为它支持 CSV 格式的数据库导出。
  • 所以简而言之,问题是PHP代码没有被Web服务器执行。其他 PHP 文件是否有效?如果是,它们之间有什么区别?
  • 对不起,我应该解释得更好一点。是的,所有其他 PHP 文件都在执行,上面的代码只是 php 页面中的一个小 sn-p,除了下载 .csv 部分外,该页面正在运行。有一个按钮可以让网站用户下载他们数据的 .csv 文件。这是无法正常工作的部分。如果您查看以下页面,您将希望看到我正在努力实现的目标code.stephenmorley.org/php/creating-downloadable-csv-files 谢谢 :-)
  • 在遍历行之后添加exit;
  • 尤金你是个传奇!!!添加出口;通过提取所有数据,但我仍然包含脚本上方的所有源代码。所以我只是将我的脚本移动到页面上的第一件事。惊人的!谢谢:-)

标签: php mysql csv download


【解决方案1】:

在循环之后添加exit; 函数调用,它将阻止其他内容呈现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多