請問怎樣實現寫入php執行後的多條記錄到txt檔案,每次重新整理就覆蓋寫入,謝謝

來源:互聯網
上載者:User
請問怎樣實現寫入php執行後的多條記錄到txt檔案,每次重新整理php頁面就覆蓋寫入新記錄到txt檔案,謝謝!

我在php中用switch case 產生了多條記錄,但是寫入txt時每次只會寫入最後一條記錄,前面的記錄被覆蓋。

如果fopen()函數用“a”的話,倒是可以寫入多條記錄,但是每次重新整理php頁面就會在txt檔案中原來的內容後面追加寫入,達不到我想要的每次重新整理php,就重新寫入txt的效果。 請問有什麼方法可以實現,謝謝!


php檔案 :

$sql="select * from log " ;
$query=mysql_query($sql);
while($rs=mysql_fetch_array($query))

switch($rs['status'])
{
case 1:
$str =$rs['id'];
$fp = fopen("test.txt", "w");
fwrite($fp,$str);
fclose($fp);
break;
}
?>


回複討論(解決方案)


$fp = fopen("test.txt", "w");
fclose($fp);
移到 while 迴圈外面

$sql="select * from log " ; $query=mysql_query($sql);$fp = fopen("test.txt", "w");while($rs=mysql_fetch_array($query)) {  switch($rs['status']) {    case 1:      $str =$rs['id'];      fwrite($fp,$str);      break;  }}fclose($fp); 

參考

$fp = fopen("t.txt","w");for($i = 0; $i < 4; $i++){fwrite($fp,$i);}fclose($fp);

謝謝協助!!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.