How can I write multiple records written to the txt file after php execution and overwrite the data after each refresh, thank you for editing this post by ltinsky at 2014-04-1::57:44. how can I write multiple records written to the txt file after php execution? every time I refresh the php page, I will overwrite the new record to the txt file, thank you! I used switch & nbsp; case in php. how can I write multiple records written to the txt file after execution in php? I will overwrite the records every time I refresh the file. thank you!
This post was last edited by ltinsky at 11:57:44
How can I write multiple records after php execution to the txt file? every time I refresh the php page, I will overwrite the new records to the txt file. thank you!
I used switch case in php to generate multiple records, but when I write a txt file, only the last record is written at a time, and the previous record is overwritten.
If the fopen () function uses "a", multiple records can be written, but each time the php page is refreshed, the original content will be appended to the txt file, I can't achieve the desired php refresh, and I will write the txt again. Is there any way to implement it? thank you!
Php file:
$ 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;
}
?>
------ Solution --------------------
Set
$ Fp = fopen ("test.txt", "w ");
Fclose ($ fp );
Move outside the while loop
$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);