Hi all,
Newbie here
Can someone help me with this. I am trying to export a table from a mysql database and save it to a csv file on my local drive. The code seems to run without a problem but I cant see a file called exported_data.txt anywhere and the system is not asking me to save or open the file.

/ */
$F = fopen('exported_data.txt' , 'w'); // open for write

$delim = "\t"; // set delim, eg tab

$res = mysql_query("SELECT * FROM users_users");
if ( ! $res )
die ( "<H3>There has been a problem and the query failed - Please contact the System Administrator. Thank you</H3>");

while ($row = mysql_fetch_row($res)) {
fwrite($F, join($delim, $row) . "\n");
}
fclose($F);

All help appreciated

Tim

    First, you have not checked to see if fopen() actually worked. There may be a permissions issue that you don't know about. You can add this after fopen():

    if(!($F)) echo "File did not open.";

    If you find out that the file actually opened, you're most of the way there! You could also check the fclose() function for success, though it may be redundant.

    Assuming that the file was actually written, I'd guess that the file was being written to a default location that isn't in your web path. Run a phpinfo() to see where it may have ended up. Or if you're using Linux:

    find -name 'exported*'

      Here's the code I use for myself. Give it a try. Hope this helps.

      <?

      include('database_connection');

      //clearing a file and starting fresh
      $file = fopen("myfile.csv","w");
      fputs($file,"");
      fclose($file);

      $sql = mysql_query("SELECT something from something where something = 'something'") or die (mysql_error());

      while ($row = mysql_fetch_array($sql)){

      $email = $row['email'];
      $sentto = $row['username'];
      $code = $row['password'];

      $array = array("$email","$sentto","$code"."\r\n");
      $comma_separated = implode(",", $array);

      $file = fopen("myfile.csv","a");
      fwrite($file,"$comma_separated");

      }

      fclose($file);

      mysql_free_result($sql);

      ?>

        Write a Reply...