writing data into text file and sql table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mainul
    New Member
    • Sep 2006
    • 51

    writing data into text file and sql table

    Hi guys i can write text file and also can read it. below are the codes.

    [PHP]
    <?PHP
    //write to a file
    $body_content=" This is my content"; // Store some text to enter inside the file
    $file_name="tes t_file.txt"; // file name
    $fp = fopen ($file_name, "w");
    // Open the file in write mode, if file does not exist then it will be created.
    fwrite ($fp,$body_cont ent); // entering data to the file
    fclose ($fp); // closing the file pointer
    chmod($file_nam e,0777); // changing the file permission.
    ?>
    [/PHP]


    [PHP]
    <?
    //reading file
    $myFile = "test_File.txt" ;
    $fh = fopen($myFile, 'r');
    //$theData = fread($fh,5);
    $theData = fread($fh, filesize($myFil e));
    fclose($fh);
    echo $theData;
    ?>
    [/PHP]

    I have a form where i have 3 fields (name, date and info). i m storing it into mysql database. now i need to store it into the database as well as also into a text flie. please show me the proper way to solve it
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    What is it you are asking: (1) how to change text in the text file or (2) how to insert it into a database and which one?

    Ronald :cool:

    Comment

    • mainul
      New Member
      • Sep 2006
      • 51

      #3
      Originally posted by ronverdonk
      What is it you are asking: (1) how to change text in the text file or (2) how to insert it into a database and which one?

      Ronald :cool:
      Hi Ronald,

      actually i have an application in php and mysql. The mysql table is 'pod' and the entity are 'id' , 'track' and description. i m inserting data into the table through a form. now i need to write those data into a text file from browser end. i dont want to delete the data from database. i want to store the data both in sql table and also in a text file (tab delimated).


      best regards
      Mainul

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        I still don't exactly know what you are looking for. The next snippet reads rows from the database and writes them to a text file. Each field separated with a tab. You can adjust it to your own needs.[php]<?php
        // name of the output file
        $myFile = "myfile.txt ";
        // Make a MySQL Connection
        $conn = mysql_connect(" xxx", "yyy", "zzz")
        or die("Could not connect: ".mysql_error() );
        mysql_select_db ("db",$conn)
        or die("Could not select db: " . mysql_error());
        // open the new output file
        $fhandle = fopen($myFile, 'w')
        or die("Error opening file.");
        // select the data from db
        $res = mysql_query("SE LECT code, name, city FROM clients");
        // write each row's data to text file
        while ($row = mysql_fetch_ass oc($res))
        fwrite ($fhandle, "{$row['code']}\t{$row['name']}\t{$row['city']}\r\n");
        // close the output file
        fclose($fhandle );
        // issue message
        echo "File $myFile written."
        ?>[/php]
        Ronald :cool:

        Comment

        Working...