gpt4 book ai didi

php - 内爆 php 数组并导出到 txt 会创建重复值

转载 作者:行者123 更新时间:2023-12-02 22:27:41 25 4
gpt4 key购买 nike

以下脚本生成包含所需数据的 txt 文件,但它在文本文件中生成了两次数据。

 mysql_connect($hostname_MySQLCon, $username_MySQLCon, "") or die(mysql_error()); 
mysql_select_db($database_MySQLCon) or die(mysql_error());
$data = mysql_query("
SELECT sales_flat_order.increment_id, sales_flat_order.gift_message_id, sales_flat_order.status, gift_message.gift_message_id, gift_message.message
FROM sales_flat_order
JOIN gift_message ON sales_flat_order.gift_message_id = gift_message.gift_message_id
WHERE sales_flat_order.gift_message_id IS NOT NULL
GROUP BY sales_flat_order.increment_id
/* AND sales_flat_order.status = 'pending' */;")
or die(mysql_error());
while($result = mysql_fetch_array( $data ))
{
$dataRow[] = implode("|", $result);
}
$theFile = 'orders-meta.txt';
if (!$handle = fopen($theFile, 'a')) {
exit;}
if (fwrite($handle, implode("\r\n", $dataRow)) === FALSE) {
echo "Cannot write to file ($theFile)";
exit;}
echo "Success, the file was written.";
fclose($handle);

txt 文件中的示例输出:

100000001|100000001|1121|1121|pending|pending|1121|gift message|gift message
100000002|100000002|1123|1123|pending|pending|1123|Gift message|Gift message

为什么它会产生每个值两次?以及如何更改它以便输出为:

100000001|1121|pending|1121|gift message
100000002|1123|pending|1123|Gift message

非常感谢任何帮助。

谢谢

最佳答案

因为您正在使用 mysql_fetch_array()而不是 mysql_fetch_row() .

mysql_fetch_array 的默认 $result_typeMYSQL_BOTH:

array mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )

这将产生这样的数组:

array(
0 => 100000001
'increment_id' => 100000001,
1 => 1121,
'gift_message_id' => 1121,
2 => 'pending',
'status' => 'pending',
3 => 1121,
4 => 'gift message',
'message' => 'gift message'
)

为什么您以如此低效的方式存储数据?为什么不这样做:

$theFile = 'orders-meta.txt';
if (!$handle = fopen($theFile, 'a')) {
exit;
}

while($result = mysql_fetch_array( $data )) {
fwrite($handle, implode("|", $result));
fwrite($handle, "\n\r");
}

fclose($handle);

关于php - 内爆 php 数组并导出到 txt 会创建重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12725845/

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