hi everyone
I want to know
how to export table data from a mysql database into text file using php?
thanks in advance
Lary
hi everyone
I want to know
how to export table data from a mysql database into text file using php?
thanks in advance
Lary
There are several ways you can do this. The shortest is:
[php]mysql_query(“SELECT Field1, Field2 INTO OUTFILE ‘/home/public_html/tmp/data.csv’ FIELDS TERMINATED BY ‘,’ LINES TERMINATED BY ‘\n’ FROM MyTable”);[/php]
But this will work if writing permissions are set for MySQL to the corresponding directory on server.
Second way, is just creating a file from your php script and writing there all the table records in a loop.
Here is an example:
[php]<?php
// read data from a database table
$r = mysql_query(“SELECT Field1, Field2 FROM MyTable ORDER BY Field1”);
if(mysql_num_rows($r)){
// create file for output
$fp=fopen('tmp/data.csv','w');
if($fp){
for($i=0;$i<mysql_num_rows($r);$i++){
$f=mysql_fetch_array($r);
$line=$f["Field1"].", ".$f["Field2"];
if(trim($line)!='') fputs($fp,$line."\n");
}
}
fclose($fp);
}
?>[/php]
thank you so much