fputcsv:单独的值(字段)与&#34 ;;"并删除"引用"外壳

时间:2015-11-02 10:29:43

标签: php fputcsv

感谢社区,我能够创建一个脚本,使我能够将数据库表中的数据导出到csv文件中。

"Article ID",Shoppergroupname,"Promotion Price",VAT-Code,"Article Currency","Promotion Start Date","Promotion End Date"
10192605,"Aucun Groupe Clients",2.86,2,27,07.01.2010,30.12.2999
10192689,"Aucun Groupe Clients",3.33,2,27,07.01.2010,30.12.2999
10193415,"Aucun Groupe Clients",2.40,1,27,07.01.2010,30.12.2999
10193431,"Aucun Groupe Clients",7.83,1,27,07.01.2010,30.12.2999

我希望我的数据导出时不会使用引号括号和值|字段与;而不是,分隔。

如果有人能告诉我如何实现这一目标,我将不胜感激。

提前多多感谢。

干杯,

马克

我的脚本如下:

// Create connection
$conn = new mysqli($databasehost, $databaseusername, $databasepassword, $databasename);

// Check connection
if (mysqli_connect_errno()) {
  exit('Connect failed: '. mysqli_connect_error());
 }

 echo "Connected successfully | ";

// Create filename
date_default_timezone_set('Europe/Zurich');
$today = date("Y-m-d-G-i-s"); 
$csvname = "WS_PRICE_IMPORT_".$today.".csv";
$csvfullname = '/var/.../'.$csvname; 

// Create a file pointer connected to the output stream
$output = fopen($csvfullname, 'w');

// Output the column headings
fputcsv($output, array('Article ID', 'Shoppergroupname', 'Promotion Price', 'VAT-Code', 'Article Currency', 'Promotion Start Date', 'Promotion End Date'));

// Fetch the data
$sql = "SELECT `Article ID`, `Shoppergroupname`, `Promotion Price`, `VAT-Code`, `Article Currency`, `Promotion Start Date`, `Promotion End Date` FROM jos_temppriceimport";

$result = $conn->query($sql);

if (!$result) {
   echo "Unable to execute query in the database : " . mysql_error();
exit;
}

if ($result->num_rows == 0) {
echo "No record found, no record to export in CSV.";
exit;
}

// Loop over the rows, outputting them
while ($row = $result->fetch_row()) fputcsv($output, $row);

1 个答案:

答案 0 :(得分:0)

如果您已经查看了函数fputcsv的手册,那么您会发现这一点:

fputcsv ( resource $handle , array $fields [, string $delimiter = "," [, string $enclosure = '"' [, string $escape_char = "\" ]]] )

所以对你来说代码意味着:

while ($row = $result->fetch_row()) fputcsv($output, $row, ";", " ");

来源:http://php.net/manual/en/function.fputcsv.php