Hello mates, I need help about writing mysql results to a text file. I stuck in this code, it only write "array" to a text file. $myFile = "test.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); function data_dump( $query ) { $results = mysql_query( $query ); $data = array(); while ( $row = @mysql_fetch_assoc( $results ) ) $data[] = $row; return $data; } $stringData = data_dump( $query )."\n"; fwrite($fh, $stringData); fclose($fh); PHP:
If you want to read it back into the variable later, use serialize(). If you just want to store the contents for someone to look at, use join() with a friendly delimiter like ' | '.
SELECT id, name, email INTO OUTFILE '/tmp/result.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY ‘\\’ LINES TERMINATED BY '\n' FROM users WHERE 1 It's an example, update the query in it
Try this $myFile = "test.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); while ($result = mysql_fetch_row($query)) { $str=''; foreach($result as $value) { if ($value=== NULL OR is_null($value)) $value = 'NULL' ; $str= $str.$value.','; } fwrite($fh, $str."\n"); } fclose($fh); Code (markup):