csv php mysql data export - all data is being exported in one column

后端 未结 1 685
自闭症患者
自闭症患者 2021-01-28 02:12

I need Name,Given phonenumber mobile notes location to all be in separate columns on my csv export using the php script below. The current code exports all of the selected data

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-28 03:10

    Use this code

    // output headers so that the file is downloaded rather than displayed
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=data.csv');
    
    // create a file pointer connected to the output stream
    $output = fopen('php://output', 'w');
    
    // output the column headings
    fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));
    
    // fetch the data
    mysql_connect('localhost', 'username', 'password');
    mysql_select_db('database');
    $rows = mysql_query('SELECT field1,field2,field3 FROM table');
    
    // loop over the rows, outputting them
    while ($row = mysql_fetch_assoc($rows)) 
    
       fputcsv($output, $row);
    

    0 讨论(0)
提交回复
热议问题