By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,642 Members | 1,696 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,642 IT Pros & Developers. It's quick & easy.

writing data into text file and sql table

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

//write to a file
$body_content="This is my content"; // Store some text to enter inside the file
$file_name="test_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_content); // entering data to the file
fclose ($fp); // closing the file pointer
chmod($file_name,0777); // changing the file permission.

//reading file
$myFile = "test_File.txt";
$fh = fopen($myFile, 'r');
//$theData = fread($fh,5);
$theData = fread($fh, filesize($myFile));
echo $theData;

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
Jan 24 '07 #1
Share this Question
Share on Google+
3 Replies

Expert 2.5K+
P: 4,258
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:
Jan 24 '07 #2

P: 51
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
Jan 25 '07 #3

Expert 2.5K+
P: 4,258
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());
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("SELECT code, name, city FROM clients");
// write each row's data to text file
while ($row = mysql_fetch_assoc($res))
fwrite ($fhandle, "{$row['code']}\t{$row['name']}\t{$row['city']}\r\n");
// close the output file
// issue message
echo "File $myFile written."
Ronald :cool:
Jan 26 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.