Jump to content

Recommended Posts

Hi everyone,

 

I was wondering if it was possible to just export certain fields from a mydql database to be written to a csv file?

 

Here is the snippet that writes to the file:

 

$pfw_file_name = "mailinglist.csv";
$pfw_first_raw = "textfield,textfield2,textfield3,emailList\r\n";
$pfw_values = "$textfield,$textfield2,$textfield3,$emailList\r\n";
$pfw_is_first_row = false;
if(!file_exists($pfw_file_name))
{
$pfw_is_first_row = true ;
}
if (!$pfw_handle = fopen($pfw_file_name, 'a+')) {
die("Cannot open file ($pfw_file_name)");
exit;
}
if ($pfw_is_first_row)
{
  if (fwrite($pfw_handle, $pfw_first_raw ) === FALSE) {
  die("Cannot write to file ($pfw_filename)");
  exit;
  }
}
if (fwrite($pfw_handle, $pfw_values) === FALSE) {
  die("Cannot write to file ($pfw_filename)");
  exit;
}
fclose($pfw_handle);

 

Can I just add SELECT * FROM entry_form WHERE newsletter='y'; and get the results that I want? If I can, where should I add this line to the code?

 

Thanks!

<?

$pfw_file_name = "mailinglist.csv";

if (!$pfw_handle = fopen($pfw_file_name, 'a+')) {

die("Cannot open file ($pfw_file_name)");

exit;

}

$pfw_first_raw = "textfield,textfield2,textfield3,emailList\r\n";

$pfw_is_first_row = false;

if(!file_exists($pfw_file_name))

{

$pfw_is_first_row = true ;

}

if ($pfw_is_first_row)

{

  if (fwrite($pfw_handle, $pfw_first_raw ) === FALSE) {

  die("Cannot write to file ($pfw_filename)");

  exit;

  }

}

 

 

$sql = "SELECT * FROM entry_form WHERE newsletter='y' ";

$re = mysql_query($sql);

while($data = mysql_fetch_array($re))

{

$textfield = $data['fieldName'];  etc

$pfw_values = "$textfield,$textfield2,$textfield3,$emailList\r\n";

if (fwrite($pfw_handle, $pfw_values) === FALSE) {

  die("Cannot write to file ($pfw_filename)");

  exit;

}

}

 

 

 

fclose($pfw_handle);

 

?>

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.