【问题标题】:exporting a mysql query in php version 4 to a csv将 php 版本 4 中的 mysql 查询导出到 csv
【发布时间】:2016-10-20 14:11:31
【问题描述】:

编辑我原来的帖子。我找到了答案!!!!在帮助下:)

我现在通过使用下面的代码来完成这项工作,感谢 cmets 中对此的建议:

<?php

$f = fopen('incident_csv\test.csv', 'w');

$query = "
select column1, column2, column3
from table
where columns = values
";

$var1 = mysql_query($query, $database connection variable);

/* From Monkey Zeus */

$csv_lines = array();

// Loop your records from the DB
while ($row = mysql_fetch_assoc($var1)){

$columns = array();

// Loop each column for the row
foreach($row as $k=>$v){
// Surround column item in double-quotes and escape double-quotes with double-double-quotes
$columns[] = '"'.str_replace('"', '""', $v).'"';
}

// Join on a comma
$csv_lines[] = implode(',', $columns);
}

// Create full CSV file by joining on a newline
$csv_file_string = implode("\n", $csv_lines);

/* From Monkey Zeus */  

fwrite($f, $csv_file_string);

?>

【问题讨论】:

  • PHP 4 自 2008 年 8 月以来就没有安全更新。逃跑吧。跑得很远,很远。
  • fpost是哪个函数?你写了吗?而且您没有在任何地方保存从fopen 返回的文件资源。您将无法以这种方式使用它。
  • @Quentin 很可能 OP 正在旧系统的范围内工作,该系统可能已经扩展到影响公司基础架构的多个方面。只要这不是用于为知名网站提供动力,那么 PHP4 应该可以完美地用于实现既定目标。

标签: php csv php4


【解决方案1】:

你可以这样做:

$csv_lines = array();

// Loop your records from the DB
while ($row = mysql_fetch_assoc($var1)){

    $columns = array();

    // Loop each column for the row
    foreach($row as $k=>$v){
        // Surround column item in double-quotes and escape double-quotes with double-double-quotes
        $columns[] = '"'.str_replace('"', '""', $v).'"';
    }

    // Join on a comma
    $csv_lines[] = implode(',', $columns);

    // Write to the file right away. You are using PHP4 so I imagine system memory is not plentiful
    // If you use this then there is no need for the "$csv_lines[] =" from above
    // nor the $csv_file_string after the while loop
    // fwrite($f, implode(',', $columns)."\n");
}

// fclose($f); // If you choose to write to the file during the while loop then close the file handle when you are finished

// Create full CSV file by joining on a newline
$csv_file_string = implode("\n", $csv_lines);

【讨论】:

  • 感谢@MonkeyZeus 的这个建议,我设法通过将其拼接到我的原始查询中并将“mysql_fetch_array”行更改为“mysql_fetch_assoc”以删除 CSV 文件中的重复项。
  • 不错!很高兴我能帮上忙。但是,请使用fwrite($f, implode(',', $columns)."\n"); 查看我的更新,因为我认为您使用 PHP4 的服务器没有太多内存,因此可能需要写入磁盘。
【解决方案2】:

您可以简单地使用 arrayToCsv 函数:

function arrayToCsv($array) {
    $string = array();

    foreach ($array as $field) {
        $string[] = implode(';', $field);
    }

    return implode("\n", $string);
}

$a = array(
    array('First Field of First Line', 'Second Field of First Line'),
    array('First Field of Second Line', 'Second Field of Second Line')
);
fwrite($f, arrayToCsv($a));

但是,请记住将您的 $f 设置为 $f = fopen('file location', 'w');

希望我能有所帮助。

【讨论】:

  • CSV 中的 C 通常代表 Comma,而不是分号。
  • Excel 只能用分号正确打开它。 ;-) 但是,如有必要,很容易更改。
  • 感谢您对此的建议,但我努力让这种方法发挥作用,我仍然会尝试,因为知道如何以两种方式做事总是好的 :)
  • 没有错误,即使页面完成也无法填充 csv 文件。我对 php 也很陌生,并且努力改变它以满足我的需求。
  • 你能发布你的完整代码吗?你有没有像我说的那样改变你做fopen的部分?
猜你喜欢
  • 1970-01-01
  • 2015-02-01
  • 1970-01-01
  • 2013-04-29
  • 2017-12-31
  • 1970-01-01
  • 2018-02-04
  • 2013-09-15
  • 1970-01-01
相关资源
最近更新 更多