gpt4 book ai didi

PHP/MySQL 使用管道分隔符获取并转换为 csv

转载 作者:太空宇宙 更新时间:2023-11-03 12:22:36 24 4
gpt4 key购买 nike

我有一个将 mysql 查询导出到 | 的 PHP 脚本定界文件。除了最后一个“列”之外,这个工作很好是附加一个 |在我不需要时使用管道。

$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|

如何避免在 Phone 之后有结束管道|所以它看起来像这样:

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

最佳答案

使用这个。

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";
}

关于PHP/MySQL 使用管道分隔符获取并转换为 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19141208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com