2013-10-02 112 views
0

我有一個PHP腳本將mysql查詢導出到|分隔文件。除了最後一個「列」之外,這個工作非常棒,正在追加一個|當我不需要它的時候。PHP/MySQL獲取並轉換爲帶管道分隔符的csv

$values = mysql_query("SELECT ColumnA AS Name, ColumnB AS Address, ColumnC AS Phone FROM Table1"); 
$row = 0; 
while ($rowr = mysql_fetch_assoc($values)) 
{ 
    if ($row == 0) 
    $row++; 
    $csv_output .= "id|"; 
    foreach($rowr as $name => $value) 
    { 
    $csv_output .= $value."|"; 
    } 
     $csv_output .= "\n";  
} 

最終的結果看起來像

Name|Address|Phone| 
Name|Address|Phone| 
Name|Address|Phone| 

如何避免電話後,有結束管|所以它看起來像這樣:

Name|Address|Phone 
Name|Address|Phone 
Name|Address|Phone 

回答

3

使用這一個。

while ($rowr = mysql_fetch_assoc($values)) 
{ 
    if ($row == 0) 
    $row++; 
    $csv_output .= "id|"; 
    foreach($rowr as $name => $value) 
    { 
    $csv_output .= $value."|"; 
    } 
     $csv_output = substr($csv_output,0,-1); 
     $csv_output .= "\n";  
} 
+0

完美,謝謝! –

相關問題