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? Quote Link to comment 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>'; } Quote Link to comment Share on other sites More sharing options...
the182guy Posted April 5, 2010 Share Posted April 5, 2010 Or fputcsv Quote Link to comment 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 ? Quote Link to comment 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? Quote Link to comment 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,niamhoconnor95@gmail.com,0872301662,1995-11-14,Female,261,Sorcha ,Mccarthy,smsexy2k9@hotmail.com,0866636166,1992-05-31,Female,ii Hope I Win Because Its Always A Good Craiic With The Lads x262,adam,hassett,adamhassett@ymail.com,0851702845,1955-05-03,Male,fkken mint lad263,Chloe,McCarthy,Cmacca15@hotmail.com,0857160817,1992-05-05,Female,Please I never win anything264,jonathan ,foley,foleyloveskatie@gmail.com,0876481638,1994-10-10,Male,265,mark,o shea,markoshea93086@hotmail.com,0862243677,1993-01-19,Male,can i plzzzzzzz hv da tickets kev cause im ur best dj hahaha266,Amy,Peppard,amyx0x07@hotmail.com,,1993-08-11,Female,267,Amy,Peppard,amyx0x07@hotmail.com,,1993-08-11,Female,268,melissa,mccarthy,mel-mcc-95@gmail.com,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 Quote Link to comment 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 Quote Link to comment 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.