title: MySQLi export table to CSV
body: <p>I've been trying for many days to export from a query some values in <code>.csv</code> with MySQLi, but I have a problem when the export is made, everything is exporting to a single column, instead I need that the export should make like SQL code (I have 3 selected columns and I need 3 exported columns in <code>.cvs</code>).
  Bellow you can find two codes:</p>
  
  <p>1) This code creates a data base, table and insert the values:</p>
  
  <pre><code>&lt;?php
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "myDB";
  // Create connection
  $conn = new mysqli($servername, $username, $password);
  // Check connection
  if ($conn-&gt;connect_error) {
      die("Connection failed: " . $conn-&gt;connect_error);
  }
  // Create database
  $sql = "CREATE DATABASE myDB";
  if ($conn-&gt;query($sql) === TRUE) {
      echo "Database created successfully";
  } else {
      echo "Error creating database: " . $conn-&gt;error;
  }
  // Create connection pt a crea tabela
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
      die("Connection failed: " . $conn-&gt;connect_error);
  }
  // sql to create table
  $sql = "CREATE TABLE MyGuests (
      id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
      firstname VARCHAR(30) NOT NULL,
      lastname VARCHAR(30) NOT NULL,
      email VARCHAR(50),
      reg_date TIMESTAMP
  )";
  if ($conn-&gt;query($sql) === TRUE) {
      echo "Table MyGuests created successfully";
  } else {
      echo "Error creating table: " . $conn-&gt;error;
  }
  // Create connection pt a insera valori
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
      die("Connection failed: " . $conn-&gt;connect_error);
  }
  // Insert value in db
  $sql = "INSERT INTO MyGuests (firstname, lastname, email)
      VALUES ('John', 'Doe, Mark', 'john@example.com'),
      ('Mary', 'Moe-Johny', 'mary@example.com'),
      ('Julie', 'Dooley', 'julie@example.com')";
  if ($conn-&gt;query($sql) === TRUE) {
      echo "New record created successfully";
  } else {
      echo "Error: " . $sql . "&lt;br&gt;" . $conn-&gt;error;
  }
  $conn-&gt;close();
  ?&gt;
  </code></pre>
  
  <p>2) This code is for the export in <code>.csv</code>:</p>
  
  <pre><code>&lt;?php
  //header to give the order to the browser
  header('Content-Type: text/csv');
  header('Content-Disposition: attachment;filename=export.csv');
  $servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "myDB";
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
      die("Connection failed: " . $conn-&gt;connect_error);
  }
  $sql = "SELECT id, firstname, lastname FROM MyGuests";
  $result = $conn-&gt;query($sql);
  if ($result-&gt;num_rows &gt; 0) {
      // output data of each row
      while($row = $result-&gt;fetch_assoc()) {
          echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "&lt;br&gt;";
      }
  } else {
      echo "0 results";
  }
  $conn-&gt;close();
  ?&gt;
  </code></pre>
  
  <p>I'm using this XAMP version:</p>
  
  <blockquote>
    <p>Apache/2.4.18 (Win32) OpenSSL/1.0.2e PHP/7.0.1</p>
    
    <p>Database client version: libmysql - mysqlnd 5.0.12-dev - 20150407 - $Id: 7e72f9690b1498a1bead7a637c33a831c0d2f655 $</p>
    
    <p>PHP extension: mysqli Documentation</p>
    
    <p>PHP version: 7.0.1</p>
  </blockquote>
  
  <p>This version creates some problems for the MySQL code, that's why I prefer the code in MySQLi.
  Thank you very much!</p>