LuciBKen Posted June 20, 2007 Share Posted June 20, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/56365-extracting-certain-field-data-to-a-csv-file/ Share on other sites More sharing options...
aniesh82 Posted June 20, 2007 Share Posted June 20, 2007 <? $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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/56365-extracting-certain-field-data-to-a-csv-file/#findComment-278438 Share on other sites More sharing options...
LuciBKen Posted June 20, 2007 Author Share Posted June 20, 2007 Thanks! I'll give that a shot. Quote Link to comment https://forums.phpfreaks.com/topic/56365-extracting-certain-field-data-to-a-csv-file/#findComment-278440 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.