Jump to content

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

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.