我想从apriori_main表中获取一些整数并将它们作为逗号分隔值存储到文本文件中。对于每次迭代,我都会file_put_contents
在下一行写入数据。使用fwrite
给出相同的结果。
我想要在文本文件中的输出是:
1,2,3,4
但我得到的输出是:
1
,2
,3
,4
这是代码片段:
$y="";
$stmt='SELECT category FROM apriori_main where id='.$id.'';
$nRows = $conn->query('select count(category) from apriori_main where id='.$id.'')->fetchColumn();
echo $nRows;
$file = "/opt/lampp/htdocs/ghi.txt";
$f = fopen($file, 'a+'); // Open in write mode
$count=1;
foreach($conn->query($stmt) as $row)
{
if($count!=$nRows)
{
$user = $row['category']."\n";
$y=$user; $y=$y.",";
$str=$y; echo $y;
$count=$count+1;
}
else
{
$user = $row['category']."\n";
$y=$user; $str=$y; echo $y;
}
file_put_contents($file, $str, FILE_APPEND);
}
fclose($f);