title: Output double quote to csv file from PHP
body: <p>I know there are some similar threads but my code doesn't look anything like those questions and am not sure how to adopt.  I think my approach can probably work, but I am just not getting the desired result.  </p>
  
  <p>I am trying to export a MySQL db to a csv file.  One field has commas in it so putting the string in double quotes allows me to reimport the csv file back into my program.  Without the quotes, the comma makes it seem like another field, so then all subsequent data is off in my reimport.</p>
  
  <p>The first line of code outputs perfect, the description field may have commas or other punctuation.  I want to put double quotes around it and have it output to my csv file.  </p>
  
  <pre><code> &lt;td&gt;&lt;?php echo $rows['model']; $csv_output .= $rows['model'] . ", ";?&gt;&lt;/td&gt;
   &lt;td&gt;&lt;?php echo $rows['description']; $csv_output .= "\"" . $rows['description'] . "\"" . ", ";?&gt;&lt;/td&gt;
  </code></pre>
  
  <p>For testing I have substituted "\"" with "5" and a 5 appears before and after my string and in the csv file.</p>
  
  <p>I am new so if it is at all possible to do it this way, it would be better than changing my approach.</p>
  
  <p>I tried the below line and added an echo to the screen and all my data, quotes included displays.</p>
  
  <pre><code> &lt;td&gt;&lt;?php echo $rows['description']; $csv_output .= '"' . $rows['description'] . '"' . ", ";?&gt;&lt;/td&gt;
  </code></pre>
  
  <p>So one complete line looks like this.</p>
  
  <pre><code> 1, 1, , , "Retiremnet Costs JE 100209 - F", 388155, 0, 1, , , 2006-06-30, A, 1, 0, 0, 113687.67, , , 
  </code></pre>
  
  <p>The quotes are there.</p>
  
  <p>But when I press a button to post to the next page where the file will be written, the data disappears and leaves me with this.</p>
  
  <pre><code>    1, 1, , ,
  </code></pre>
  
  <p>So it seems to me that there is something perhaps wrong with the file that writes the data so I'll include that.</p>
  
  <pre><code> &lt;?php
  
   $out = '';
  
   if (isset($_POST['csv_hdr'])) {
   $out .= $_POST['csv_hdr'];
   $out .= "\n";
   }
  
   if (isset($_POST['csv_output'])) {
   $out .= $_POST['csv_output'];
   }
  
   $filename = $file."_".date("Y-m-d_H-i",time());
  
   //Generate the CSV file header
   header("Content-type: application/vnd.ms-excel");
   header("Content-disposition: csv" . date("Y-m-d") . ".csv");
   header("Content-disposition: filename=".$filename.".csv");
  
   //Print the contents of out to the generated file.
   print $out;
  
   //Exit the script
   exit;
   ?&gt;
  </code></pre>
  
  <p>I figured out the problem but still don't have a solution.  If I put in </p>
  
  <pre><code> $out .= "4, 1, , , \"Cisco 1000Base-SX SFP, LC\", 708476, 0, 3, , , 2011-03-15, A, 1,         20016525, 7088398, 300.00, , , ";
  </code></pre>
  
  <p>The file writes perfectly.  I changed all of my csv_output statements so that when I echo it reads exactly the same as what I pasted over the POST variable on the POST page, but when I do that, the csv_output variable is completely empty.  </p>
  
  <p>Thanks</p>