简体   繁体   English

如何将MySQL查询的每个结果一个一个地放入文本文件?

[英]How to put each result of a MySQL query into a text file one by one?

while($row= mysql_fetch_object($result)){
    $row->products_price=$row->products_price-($row->products_price*0.05);
    echo $row->products_id ." ". $row->products_price."<br/>";
}

How can I put $row->products_id ." ". $row->products_price 我怎样才能把$row->products_id ." ". $row->products_price $row->products_id ." ". $row->products_price into a txt file with one row a time? $row->products_id ." ". $row->products_price转换为每次一行一行的txt文件?

$content = "";
while ( $row = mysql_fetch_object($result) ){
    $row->products_price = $row->products_price-($row->products_price * 0.05);
    $content .=  $row->products_id . " " . $row->products_price . "\r\n";
}
file_put_contents("filename.txt", $content, LOCK_EX);

Edit: (a version for Hikaru-Shindo) 编辑:(Hikaru-Shindo的版本)

while ( $row = mysql_fetch_object($result) ){
    $row->products_price = $row->products_price-($row->products_price * 0.05);
    file_put_contents("filename.txt", $row->products_id . " " . $row->products_price . "\r\n", FILE_APPEND | LOCK_EX);
}
$fh = fopen("myfile.txt", "w+");
while($row= mysql_fetch_object($result))
{
    $row->products_price=$row->products_price-($row->products_price*0.05);
    fwrite($fh, $row->products_id ." ". $row->products_price."\r\n");
}
fclose($fh);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM