title: Exporting data from mysql table to a .csv file
body: <p>I have a mysql table in which I store cellphone numbers of the users, and now I want to export only those numbers to a .csv file using php, and I don't want to use a comma in the end of each number. Now suppose I have the next 3 numbers stored in my table: </p>
  
  <pre><code>123456789
  +966123456789  
  00966123456789
  </code></pre>
  
  <p>Now if I used the next code:</p>
  
  <pre><code>$result = mysql_query("SELECT cellphone FROM user");
  if ($result) {
      while ($row = mysql_fetch_array($result)) {
          $cellphones .= $row["cellphone"] . ",\r\n"; //note the comma here
      }
  }
  
  $filename = "cellphones_" . date("Y-m-d_H-i");
  header("Content-type: application/vnd.ms-excel");
  header("Content-disposition: csv" . date("Y-m-d") . ".csv");
  header("Content-disposition: filename=" . $filename . ".csv");
  print $cellphones;
  exit;
  </code></pre>
  
  <p>I will get a .csv file in which I have the numbers like this:</p>
  
  <pre><code>+966123456789,
  00966123456789,
  123456789,
  </code></pre>
  
  <p>But if I used the same code without the comma like this: </p>
  
  <pre><code>    $cellphones .= $row["cellphone"] . "\r\n";
  </code></pre>
  
  <p>instead of: </p>
  
  <pre><code>    $cellphones .= $row["cellphone"] . ",\r\n"; //note the comma here
  </code></pre>
  
  <p>then numbers in the .csv file will be: </p>
  
  <pre><code>9.66123E+11
  9.66123E+11
  123456789
  </code></pre>
  
  <p>So what is the wrong and how can I get the numbers appear correctly without the comma?</p>