How to implement write to PHP after the execution of multiple records to TXT file, each refresh overwrites the write, thank you

Source: Internet
Author: User
Tags switch case
How to implement write to PHP after the execution of multiple records to TXT file, every time the PHP page is overwritten to write new records to the TXT file, thank you!

I generated multiple records in PHP with a switch case, but only the last record was written to TXT, and the previous record was overwritten.

If the fopen () function uses "a", it can write multiple records, but each time the PHP page refresh will be in the TXT file after the original content to write, not up to the time I want to refresh PHP, the effect of re-write txt. May I ask what method can be realized, thank you!


PHP Files:

$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
}
?>


Reply to discussion (solution)

Put
$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  

Reference

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

Thanks for the help!!

  • 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.