Getting PHP error Warning: fputcsv() expects parameter 2 to be array

后端 未结 3 1432
深忆病人
深忆病人 2021-01-28 21:00

I have mysql 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 MYS

相关标签:
3条回答
  • 2021-01-28 21:38

    Read the documentation

    fputcsv should be array of values, you have a there string.

    0 讨论(0)
  • 2021-01-28 21:56

    "Warning: fputcsv() expects parameter 2 to be array, string given inon line 17" is a very helpful message.

    What that means is that in fputcsv($fp, $val);, $val is expected to be an array and it is not.

    $val is an element of the $row array, so there's a problem with your query or the data in the database.

    0 讨论(0)
  • 2021-01-28 21:59

    You have to give it an array so try it like this

        $con=mysqli_connect("localhost","root","","mytable");
    
        if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }
    
    
         $sql = "SELECT user_id FROM tbl_users";
          $users_profile_user_id = mysqli_query($con, $sql); 
    
          $fp = fopen("user_profile_id.csv", "w");
    
        while($row = mysqli_fetch_array($users_profile_user_id, MYSQLI_ASSOC)){
          fputcsv($fp, $row);
        }
    
        fclose($fp);
    
    0 讨论(0)
提交回复
热议问题