从php字符串创建CSV文件

时间:2018-03-21 13:06:26

标签: php

我想使用php创建一个CSV文件,每行末尾都有一个分号。

该文件应该像这样:

Smith,"Andreas",";"
Smith,"John",";"

我不能在行尾给分号。我尝试了各种各样的屏蔽,在网上阅读了关节,但我无法让它发挥作用。

这是我使用的代码:

$datei1 = fopen($file1,"w");

$query = "SELECT * FROM customers ORDER BY cust_name";
$cust_name = $row['cust_name'];
$cust_firstname = $row['cust_firstname'];


$semicolon = ";";

$data = '"'.$cust_name.'","'.$cust_firstname.'","'.$semicolon.'"';

fwrite($datei1, $data);
fwrite($datei1, "\r\n");

1 个答案:

答案 0 :(得分:1)

我不认为分号是一个问题,它只显示第一行,因为你没有循环查询

$datei1 = fopen($file1,"w");

$data = mysql_query("SELECT * FROM customers ORDER BY cust_name",$db);

while($row = mysql_fetch_array($data)){
    $mydata = [ $row['cust_name'] , $row['cust_firstname'] , ';'];
    fputcsv($datei1,$mydata,",");
}

fclose($datei1);