Jump to content

Writing to .txt file


graham23s

Recommended Posts

Hi Guys,

 

what i was trying to do is loop a load of data from mysql and write it to a .txt file i have:

 

<?php
     $host = "localhost";       
     $user = "root"; 
     $pass = ""; 
     $data = "test"; 

     // Connect and select the database //
     $conn = mysql_connect($host, $user, $pass) or die(mysql_error()); 
       $db = mysql_select_db($data, $conn) or die(mysql_error()); 
       
     // select
     $q = "SELECT * FROM `syla_emails`";
     $r = mysql_query($q);
     
     while ($a = mysql_fetch_array($r))
     {
     
      // vars
      $email = $a['EMAIL'];
       
      $ourFileName = "testFile.txt";
      
      $ourFileHandle = fopen($ourFileName, 'w') or die ("can't open file");
      
      fwrite($ourFileHandle, $email);
      
      fclose($ourFileHandle);
      
      
     }
?>

 

so far but it only seems to write 1 email to file!

 

any help would be great

 

cheers

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/129278-writing-to-txt-file/
Share on other sites

you need to open/close the file outside of the loop...

 

<?php
     $host = "localhost";       
     $user = "root"; 
     $pass = ""; 
     $data = "test"; 

     // Connect and select the database //
     $conn = mysql_connect($host, $user, $pass) or die(mysql_error()); 
       $db = mysql_select_db($data, $conn) or die(mysql_error()); 
       
     // select
     $q = "SELECT * FROM `syla_emails`";
     $r = mysql_query($q);
     
     $ourFileName = "testFile.txt";
      
     $ourFileHandle = fopen($ourFileName, 'w') or die ("can't open file");

     while ($a = mysql_fetch_array($r))
     {
      // vars
      $email = $a['EMAIL'];
      fwrite($ourFileHandle, $email);
     }
     fclose($ourFileHandle);
?>

Link to comment
https://forums.phpfreaks.com/topic/129278-writing-to-txt-file/#findComment-670230
Share on other sites

ah thanks mate :)

 

1 more thing! how would i start a new line after each email? i did:

 

<?php
     $host = "localhost";       
     $user = "root"; 
     $pass = ""; 
     $data = "test"; 

     // Connect and select the database //
     $conn = mysql_connect($host, $user, $pass) or die(mysql_error()); 
       $db = mysql_select_db($data, $conn) or die(mysql_error()); 
       
     // select
     $q = "SELECT * FROM `syla_emails`";
     $r = mysql_query($q);
     
     $ourFileName = "testFile.txt";
      
     $ourFileHandle = fopen($ourFileName, 'w') or die ("can't open file");

     while ($a = mysql_fetch_array($r))
     {
      // vars
      $email = $a['EMAIL'];
      
      // string to write
      $string = "E-MAIL: [ $email ]\n";
      
      fwrite($ourFileHandle, $string);
     }
     
     fclose($ourFileHandle);
?>

 

but it doesn't recognise the \n for some reason!

 

thanks mate

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/129278-writing-to-txt-file/#findComment-670235
Share on other sites

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.