tags:

views:

71

answers:

1

Hi

Could any one explain me how to create a text file using php where the records should be from mysql

thanks in advance.

+1  A: 

1) open a file in write mode:

   $myFile = "testFile.txt";
   $fo = fopen($myFile, 'w') or die("can't open file");

2) Write mysql query and fetch its data

   $data_query=mysql_query("SELECT name,age from table");
   while($data=mysql_fetch_array($data_query))
   $stringData.="Name: ".$data['name']." Age:".$data['age']."\n";

3) Write data into the file

   fwrite($fo, $stringData);

4) Close file

   fclose($fo);
nik
hi.. thanks nik. its working fine. But how to merge multiple records. thanks for the answer
Fero
Or write the data directly (using multiple fwrite) to the file without concatenating in php if you have larger data sets.
Eiko
It depends on your data, check my edit done for the data of user's name and age, the .(dot) after $stringData will append multiple records Also the point made by Eiko is correct, its just the way you like to write your code
nik
thanks got it... :-)
Fero
is it possible to create a table structure and store the data
Fero
Then you need to create .csv file rather then txt file.
nik