graham23s Posted October 20, 2008 Share Posted October 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/129278-writing-to-txt-file/ Share on other sites More sharing options...
rhodesa Posted October 20, 2008 Share Posted October 20, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/129278-writing-to-txt-file/#findComment-670230 Share on other sites More sharing options...
graham23s Posted October 20, 2008 Author Share Posted October 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/129278-writing-to-txt-file/#findComment-670235 Share on other sites More sharing options...
wildteen88 Posted October 20, 2008 Share Posted October 20, 2008 You may want to use \r\n instead. Quote Link to comment https://forums.phpfreaks.com/topic/129278-writing-to-txt-file/#findComment-670237 Share on other sites More sharing options...
rhodesa Posted October 20, 2008 Share Posted October 20, 2008 You may want to use \r\n instead. yeah, if you are opening it in notepad on a windows machine, you will probably need the \r\n Quote Link to comment https://forums.phpfreaks.com/topic/129278-writing-to-txt-file/#findComment-670242 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.