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

时间:2011-12-05 09:48:55

标签: php

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

如何将$row->products_id ." ". $row->products_price放入一行一行的txt文件中?

2 个答案:

答案 0 :(得分:3)

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

编辑:( 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);
}

答案 1 :(得分:0)

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