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?

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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

Link to comment
Share on other sites

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
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.