Jump to content

how to make a CSV file from emails in a database


kevinkhan

Recommended Posts

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?

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>';
}

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 ?

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

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

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.