简体   繁体   English

使用PHP将MySQL表导出到CSV的未定义错误

[英]Undefined error using PHP to export MySQL table to CSV

I am using this snippet I found from an old post to download a MySQL table to CSV. 我正在使用从旧帖子中找到的片段将MySQL表下载到CSV。 It downloads the CSV fine, but I always get an 'undefined' error in with no other details that displays in my browser window. 它可以下载CSV格式的文件,但是我总是在浏览器窗口中看到“未定义”错误,而没有其他详细信息。 The CSV file does not contain any data about the error mixed in with the text. CSV文件不包含有关与文本混合的错误的任何数据。 Any help with what my error is would be much appreciated. 任何与我的错误有关的帮助将不胜感激。

I have tried testing my SQL statement, and that works fine. 我已经尝试测试我的SQL语句,并且效果很好。

$result = mysql_query("SELECT * FROM employees");
if (!$result) die('Couldn\'t fetch records');
$num_fields = mysql_num_fields($result);
$headers = array();
for ($i = 0; $i < $num_fields; $i++) {
    $headers[] = mysql_field_name($result , $i);
}
$fp = fopen('php://output', 'w');
if ($fp && $result) {
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="employeelist.csv"');
    header('Pragma: no-cache');
    header('Expires: 0');
    fputcsv($fp, $headers);
    while ($row = mysql_fetch_assoc($result)) {
        fputcsv($fp, array_values($row));
    }
    die();
}

Sorry for the initial question, but I guess it was unrelated to my code somewhat. 很抱歉第一个问题,但我想它与我的代码无关。 I think I found my problem. 我想我找到了问题。

The page was being linked from JQuery mobile, and even though it as a standard HTML page. 该页面是通过JQuery mobile链接的,即使它是标准HTML页面也是如此。 Because of the page transitions with JQuery mobile, I think that is causing the message. 由于JQuery mobile的页面转换,我认为这是导致消息的原因。 Sorry for not considering that. 很抱歉没有考虑这一点。 I did not consider that as a pertinent detail. 我不认为这是有关的细节。 Thanks for all your suggestions. 感谢您的所有建议。

Try changing your 'die' condition to this: 尝试将您的“死亡”条件更改为此:

if (!$result) die( echo mysql_error() );

That will at least provide you with a little information to go on. 至少将为您提供一些继续的信息。 You probably have more to troubleshoot after that, but you gotta start somewhere, and that's the beginning. 在那之后,您可能还需要进行更多故障排除,但是您必须从某个地方开始,这就是开始。

Step 2 for troubleshooting: use print_r( $headers ) right after the first for loop 第2步进行故障排除:在第一个for循环之后立即使用print_r( $headers )

Step 3 for troubleshooting: start printing debugging output within the while loop if neither of the first steps reveal anything useful. 解决问题的第3步:如果前两个步骤都没有发现有用的信息,请在while循环中开始打印调试输出。 For example, use something like print_r( array_values( $row ) ) inside of that loop. 例如,在该循环内使用诸如print_r( array_values( $row ) )东西。

My original post should have more clearly stated that it was the first step in troubleshooting because you haven't provided any information about what you have done in your attempts to troubleshoot other than run the code. 我的原始帖子应该更清楚地表明,这是进行故障排除的第一步,因为除了运行代码外,您没有提供任何有关尝试进行故障排除时所执行操作的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM