title: CSV file from MySQL using PHP with a OOP aproach (mysqli->connect, etc.)
body: <p>I'm trying to create a csv file direct from a mysql table using php with an OOP approach. Here is my code: </p>
  
  <pre><code>$mysqli = new mysqli($host, $user, $pass, $database);
  
  // Checking connection.
  if ($mysqli-&gt;connect_errno) {
      printf("Connect failed: %s\n", $mysqli-&gt;connect_error);
      exit();
  }
  
  $query = 'SELECT * FROM table';
  $result = $mysqli-&gt;query($query);
  
  while ($row = $result-&gt;fetch_array()) {
      $rows[] = $row;
  }
  
      $array = "";
  foreach($rows as $row) {
      $array .= "\"".$row["genus"].",".$row["species"]."\",";
  }
  
  $list = array($array);
  
  header('Content-type: application/ms-excel');
  header('Content-Disposition: attachment; filename=export.csv');
  
  $file = fopen("php://output","w");
  
  foreach ($list as $line) {
      fputcsv($file,explode(',',$line));
  }
  
  fclose($file);
  // Free result set.
  $result-&gt;close();
  
  // Close connection.
  $mysqli-&gt;close();
  </code></pre>
  
  <p>My problem is with the following part, when creating the array:</p>
  
  <pre><code>    $array = "";
  foreach($rows as $row) {
      $array .= "\"".$row["genus"].",".$row["species"]."\",";
  }
  </code></pre>
  
  <p>because when I do a print_r ($list); I don't receive the expected array:
  Array ( [0] => "homo,sapiens", [1] => "homo,erectus",
  [2] => "homo,ergaster", ) </p>
  
  <p>but this one:
  Array ( [0] => "homo,sapiens","homo,erectus","homo,ergaster", ) </p>
  
  <p>and moreover when I create the array manually</p>
  
  <pre><code>$list = array("homo,sapiens","homo,erectus","homo,ergaster");
  </code></pre>
  
  <p>it works just fine and I receive the correct csv file.</p>
  
  <p>I would appreciate if someone could help me, explain me what I'm doing wrong and what I should do to fix the code.</p>