Data from Mysql as CSV file
Posted: Sat Apr 19, 2003 7:26 am
I want to write what I thought would be simple function for CSV dumping of values.
everything works fine except it's inserting the $fin_str into the file as
$r=['field_name'] text instead of the actual values.
can anyone see what I'm doing wrong?
everything works fine except it's inserting the $fin_str into the file as
$r=['field_name'] text instead of the actual values.
Code: Select all
<?php
$table = 'products';
$fields = mysql_query( "select * from $table");
// generate field list and $fin_str string
$columns = mysql_num_fields( $fields );
for ( $i = 0; $i < $columns; $i++ ){
$part_str = '$r[' . "'" . mysql_field_name( $fields, $i ) . "']" ;
$str = $str . $part_str . ",";
//also tried
//$part_str = "$r[" . "'" . mysql_field_name( $fields, $i ) . "']" ;
//but got unexpected "/" error
}
$fin_str = substr( $str, 0, -3 );
// write file of data
$fp = fopen ("data/" . "$table.csv", "a");
while ($r = mysql_fetch_array($fields))
{
fwrite($fp,$fin_str."\n");
}
fclose($fp);
?>