当前位置:Gxlcms > 
PHP教程 > 
请问怎样实现写入php执行后的多条记录到txt文件,每次刷新就覆盖写入,谢谢
                     
                    
                        请问怎样实现写入php执行后的多条记录到txt文件,每次刷新就覆盖写入,谢谢
                        
                            时间:2021-07-01 10:21:17
                            帮助过:16人阅读
							                        
                     
                    
                    请问怎样实现写入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);    谢谢帮助!!