kevinkhan Posted April 5, 2010 Share Posted April 5, 2010 I have this code so far but cant figure out how to make a CSV file $conn = mysql_connect($dbHost, $dbUser, $dbPass) or die('Error connecting to mysql'); mysql_select_db($dbName); $query="SELECT * FROM user"; $result=mysql_query($query); $myFile = "CSV.txt"; while($row = mysql_fetch_array($result)) { echo $row['email'].","; } /* $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = $row['email'].","; fwrite($fh, $stringData); fclose($fh); */ can anybody help me out? Link to comment https://forums.phpfreaks.com/topic/197651-how-to-make-a-csv-file-from-emails-in-a-database/ Share on other sites More sharing options...
ignace Posted April 5, 2010 Share Posted April 5, 2010 function array2csv($array, $separator = ',') { return implode($separator, array_values($array)); } $query = 'SELECT * FROM user'; $result = mysql_query($query); $lines = array(); $include_headers = true; while ($row = mysql_fetch_assoc($result)) { if ($include_headers) { $lines[] = array2csv(array_keys($row)); $include_headers = false; } $lines[] = array2csv($row); } if (false === file_put_contents('CSV.txt', $lines)) { echo '<p>Failed to write CSV data</p>'; } Link to comment https://forums.phpfreaks.com/topic/197651-how-to-make-a-csv-file-from-emails-in-a-database/#findComment-1037309 Share on other sites More sharing options...
the182guy Posted April 5, 2010 Share Posted April 5, 2010 Or fputcsv Link to comment https://forums.phpfreaks.com/topic/197651-how-to-make-a-csv-file-from-emails-in-a-database/#findComment-1037312 Share on other sites More sharing options...
kevinkhan Posted April 5, 2010 Author Share Posted April 5, 2010 ok this is what i have come up with $query="SELECT * FROM user"; $result=mysql_query($query); $myFile = "CSV.csv"; while($row = mysql_fetch_array($result)) { $emails .= $row['email'].","; } $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, $emails); fclose($fh); it creates a file with emails in it but gmail doesnt recognise it and i cant upload the contacts Any ideas on what i can do ? Link to comment https://forums.phpfreaks.com/topic/197651-how-to-make-a-csv-file-from-emails-in-a-database/#findComment-1037313 Share on other sites More sharing options...
ignace Posted April 5, 2010 Share Posted April 5, 2010 Any ideas on what i can do ? Have you tried mine? Link to comment https://forums.phpfreaks.com/topic/197651-how-to-make-a-csv-file-from-emails-in-a-database/#findComment-1037322 Share on other sites More sharing options...
kevinkhan Posted April 5, 2010 Author Share Posted April 5, 2010 Any ideas on what i can do ? Have you tried mine? yes i have thanks it produced this in a txt file id,firstName,lastName,email,mobile,dateOfBirth,gender,comments254,niamh,o'Connor,[email protected],0872301662,1995-11-14,Female,261,Sorcha ,Mccarthy,[email protected],0866636166,1992-05-31,Female,ii Hope I Win Because Its Always A Good Craiic With The Lads x262,adam,hassett,[email protected],0851702845,1955-05-03,Male,fkken mint lad263,Chloe,McCarthy,[email protected],0857160817,1992-05-05,Female,Please I never win anything264,jonathan ,foley,[email protected],0876481638,1994-10-10,Male,265,mark,o shea,[email protected],0862243677,1993-01-19,Male,can i plzzzzzzz hv da tickets kev cause im ur best dj hahaha266,Amy,Peppard,[email protected],,1993-08-11,Female,267,Amy,Peppard,[email protected],,1993-08-11,Female,268,melissa,mccarthy,[email protected],0852480026,1996-06-30,Female, but when i got to import in gmail it says that it cant recognize the file is there something else i can do so it will recognize the file and imports all the details? Thanks for your help Link to comment https://forums.phpfreaks.com/topic/197651-how-to-make-a-csv-file-from-emails-in-a-database/#findComment-1037326 Share on other sites More sharing options...
ignace Posted April 5, 2010 Share Posted April 5, 2010 Try: function array2csv($array, $separator = ',') { return implode($separator, array_values($array)); } $query = 'SELECT * FROM user'; $result = mysql_query($query); $lines = array(); $include_headers = true; while ($row = mysql_fetch_assoc($result)) { if ($include_headers) { $lines[] = array2csv(array_keys($row)); $include_headers = false; } $lines[] = array2csv($row); } if (false === file_put_contents('CSV.txt', implode(PHP_EOL, $lines))) { echo '<p>Failed to write CSV data</p>'; } Notice the PHP_EOL I was kinda hoping file_put_contents would do this as I passed an array apparently not Link to comment https://forums.phpfreaks.com/topic/197651-how-to-make-a-csv-file-from-emails-in-a-database/#findComment-1037391 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.