Ask how to write multiple records to the txt file after php execution, and overwrite the data every time you refresh the file. thank you!

Source: Internet
Author: User
Tags switch case
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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.