yoda69 Posted April 9, 2010 Share Posted April 9, 2010 Hey, How do I write the data from an array to a text file? This is what I got so far: $db = mysql_connect("zzz","zzz","zzz") or die("Problem connecting"); mysql_select_db("zzz") or die("Problem selecting database"); $sql = "SELECT id, color, taken FROM cells;"; $result = mysql_query($sql) or die ("Query failed"); echo "Saving the array to a file named foo.txt.\n\n"; $fp = fopen("test.txt", "w"); while ($row = mysql_fetch_assoc($result)) { foreach ($row as $val1) { echo $val1; echo " "; } echo "<br>"; } Thanks Link to comment https://forums.phpfreaks.com/topic/198100-write-array-data-from-database-into-a-text-file/ Share on other sites More sharing options...
scvinodkumar Posted April 9, 2010 Share Posted April 9, 2010 You can like this $content = ''; while ($row = mysql_fetch_assoc($result)) { ob_start(); print_r($row); $a=ob_get_contents(); ob_end_clean(); $content .= $a; } if (fwrite($fp, $content) === FALSE) { echo "Cannot write to file ($filename)"; exit; } Link to comment https://forums.phpfreaks.com/topic/198100-write-array-data-from-database-into-a-text-file/#findComment-1039389 Share on other sites More sharing options...
ignace Posted April 9, 2010 Share Posted April 9, 2010 You can write the array information using: file_put_contents('myfile', implode(PHP_EOL, array_values($row))); Link to comment https://forums.phpfreaks.com/topic/198100-write-array-data-from-database-into-a-text-file/#findComment-1039408 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.