Jump to content

Write array data from database into a text file


yoda69

Recommended Posts

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

 

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.