【发布时间】: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 应该可以完美地用于实现既定目标。