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

 

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.