2011-12-05 2 views
0
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 파일에 넣을 수 있습니까?MySQL 쿼리의 각 결과를 하나씩 텍스트 파일에 저장하는 방법은 무엇입니까?

+1

체크 PHP 매뉴얼,에 fwrite, FCLOSE 등 단지 http://php.net/fwrite 그리고 fopen에 대한 파라미터를 확인하여 – mishu

+0

고맙습니다. fwrite의 두 번째 인수를 설정하는 법을 모르겠습니다. – down321

+0

fopen의 매뉴얼 페이지를 확인하십시오. php.net/fopen .. 이미이 방법을 보여주는 답변이 있습니다 .. "a"를 사용할 수있는 파일을 열 때 기존 내용을 자르지 않으려면 많은 것들이 있습니다. 어떤 옵션이 필요한지 확인하십시오. – mishu

답변

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

편집 : (히카루 - 신도에 대한 버전) 기능 fopen에 대한

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); 
} 
+0

한 번에 하나의 행을 쓰지는 않을 것이다. –

+0

예, 그렇기 때문에 그것이 더 좋습니다! 한 줄을 다른 줄로 작성하면 진행하는 데 시간이 오래 걸립니다. – noob

+0

+1 그가 한 번에 한 행씩 물어 보지 않았다고 생각합니다. (새 행의 각 결과 행에서와 같이) ... @micha, 나는 당신의 코드를 조금 편집했습니다. – Shomz

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); 
+0

쓰기 작업 만 수행되므로 모드 "w"로 충분해야합니다. http://php.net/manual/en/function.fopen.php를 참조하십시오. –

+1

여러분은 더 현대적인 기능으로 전환하는 것이 어떻습니까? 우리는 PHP5가 !!! http://php.net/manual/en/function.file-put-contents.php – noob

+0

file_put_contents도 똑같이 작동합니다 ... 동일한 결과를 얻는 방법은 1 가지 이상입니다.) – craig1231

관련 문제