Forum Moderators: coopster

Message Too Old, No Replies

Creating a text file from sql query

Creating a text file from sql query

         

rogerg1967

8:46 am on Sep 6, 2007 (gmt 0)

10+ Year Member



I am trying to create a text file which contains all the data from a mysql database. I also need to include some information at the head of the text file. I would like to use a php page to do this where the header data would be sent from a form and the data taken from the database to create 1 text file. My first issue is how to retreive the data from the database using a php script.

Thanks in advance

Habtom

9:06 am on Sep 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Connect to DB - See example [php.net]
Query the DB - mysql_query [php.net]

//Your select statement
$query = "SELECT field1, field2 FROM table1";
$result = mysql_query($query);

//Collect your headers from the form.
$header = $_REQUEST['header1'];
$header2 = $_REQUEST['header2'];

while ($row = mysql_fetch_array($result)) {
//rearrange your data here
$yourdata = $row['field1];
}

//Write to the file

$fp = fopen("yourfile.txt", "w");
fwrite($fp, $yourdata);
fclose($fp);

Hope this gives you an idea.

Habtom