title: Getting PHP error Warning: fputcsv() expects parameter 2 to be array
body: <p>I have <code>mysql</code> db with different tables. The data between the tables are linked and I retrieve and display them by using the userid. I used the reference from PHP MYSQLi Displaying all tables in a databases. I used <code>fputcsv($fp, $val)</code> for outputting the result in <code>.csv</code> file, but I get this error:</p>
  
  <blockquote>
    <p>Warning: fputcsv() expects parameter 2 to be array** . This is my code:</p>
  </blockquote>
  
  <pre><code>&lt;?php
  
      $con=mysqli_connect("localhost","root","","mytable");
  
      if (mysqli_connect_errno()) {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
  
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
  
      $sql = "SELECT user_id FROM tbl_users";
      $users_profile_user_id = mysqli_query($con, $sql); 
      $row = mysqli_fetch_array($users_profile_user_id, MYSQLI_ASSOC);
      $fp = fopen("user_profile_id.csv", "w");
  
      foreach($row as $val) {
          fputcsv($fp, $val);
      }
  
      fclose($fp);
  
  ?&gt;
  </code></pre>
  
  <p>I also tried replacing <strong>fputcsv($fp, $val)</strong> with var_export($val) and it is showing just 1 entry from database. Now I know why is it displaying error because it is showing a string, but what is the fix to it? How can I take all the entries from database, and display all those elements in .csv file?</p>