1
感謝社區我能夠創建一個腳本,使我能夠將數據庫表中的數據導出到csv文件中。fputcsv:用「;」分隔值(字段)並刪除「報價」外殼
"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);
謝謝您的建議。它部分地工作: ''文章ID「,Shoppergroupname,」促銷價格「,增值稅代碼,」物品貨幣「,」促銷開始日期「,」促銷結束日期「' '10192605; Aucun Groupe客戶; 2.86; 2; 27; 07.01.2010; 30.12.2999;# 關於記錄,字段現在用「;」分隔,我只是在第二個字段之前和結尾處有一個縮進。 標題仍有引號,並用「;」分隔。 – marcq
它試圖爲頭,但它不起作用 '''輸出列標題' '$ treatarray = array('文章ID','Shoppergroupname','促銷價格','增值稅代碼','Article Currency','Promotion Start Date','Promotion End Date');' '$ array = implode );' – marcq
雖然這確實取消了報價封裝,但它在其位置放置了一個空間,這可能是不可取的。不幸的是,''「'不是有效的外殼。還有一些其他解決方案[這裏](http://stackoverflow.com/q/1800675/353278)。 – Jeff