PHP到CSV无法正确输出字段

I am trying to output my PHP code to a downloadable CSV file and so far i am able to do this however at the moment, the output does not place the field data into separate columns. Instead it puts everything into one column and in addition to that, it displays the postcodes in its neighbouring columns. Does anyone know why this is?

<?php
header("Content-type: text/csv");  
header("Cache-Control: no-store, no-cache");  
header('Content-Disposition: attachment; filename="filename.csv"');  

$outstream = fopen("php://output",'w');  

if ($db_found) {

// Select all fields from bodyshops_master_network table
$SQL = "SELECT * FROM bodyshops_master_network";
$result = mysql_query($SQL);

while ( $db_field = mysql_fetch_assoc($result) ) {

    // Splits the arrays
    $row = explode(",", $db_field['postcodes_covered']);

        $dealer_code = $db_field['dealer_code'];
        $dealer_name = $db_field['dealer_name'];
        $bodyshop_id = $db_field['bodyshop_id'];
        $bodyshop_name = $db_field['bodyshop_name'];
        $address1 = $db_field['address1'];
        $address2 = $db_field['address2'];
        $address3 = $db_field['address3'];
        $address4= $db_field['address4'];
        $address5 = $db_field['address5'];
        $postcode = $db_field['postcode'];
        $bo_tel = $db_field['BO_tel'];
        $bo_email = $db_field['BO_email'];
        $bc_name = $db_field['BC_name'];
        $equity_contract = $db_field['equity_contract'];

    echo "<pre>";
    foreach ($row as $value) {

        echo $dealer_code . " ";
        echo $dealer_name . " ";
        echo $bodyshop_id . " ";
        echo $bodyshop_name . " ";
        echo $address1 . " ";
        echo $address2 . " ";
        echo $address3 . " ";
        echo $address4 . " ";
        echo $address5 . " ";
        echo $postcode . " ";
        echo $bo_tel . " ";
        echo $bo_email . " ";
        echo $bc_name . " ";
        echo $equity_contract . " ";
        echo $value. "<BR>";

       fputcsv($outstream, $row, ',', '"');  
    }
        echo "</pre>";

}

mysql_close($db_handle);

} else {

print "Database NOT Found ";
mysql_close($db_handle);
fclose($outstream);  
}

?>

OK, you're doing something really weird here. You're outputting content in two ways.

First, you output content in the normal way using echo. Fair enough!

Second, you output content using fputcsv where the file handler leads to php://output. Also fair enough!

But you shouldn't do both and expect it to work seamlessly. You want a CSV file, so the best way is to output all your data using fputcsv. At the moment, some of your data is encapsulated as a CSV file, and some of it isn't. This leads to output that you don't really want.

I think you want something like this:

foreach ($row as $value) {
    $output = array(
       $dealer_code,
       $dealer_name,
       $bodyshop_id,
       $bodyshop_name,
       $address1,
       $address2,
       $address3,
       $address4,
       $address5,
       $postcode,
       $bo_tel,
       $bo_email,
       $bc_name.
       $equity_contract,
       $value
   );

   fputcsv($outstream, $output, ',', '"');  
}

fputcsv expects the data to be an array, so that's what we give it. The separate array fields will be separate columns in the CSV output. We don't echo any data: we store it in an array and then supply it to fputcsv.

The second mistake you made was to send $row rather than $value to fputcsv. This meant that you were sending all the postcodes as received from the database row.

Finally, you don't need <pre>.

On a separate note, you really should have a better database structure. If you had a table of postcodes and a separate table of bodyshops and a foreign key linking them, the code to retrieve data would be a lot simpler, as would searching and updating it in future.