删除列名称(将mysql导出为CSV)

时间:2014-12-10 10:27:26

标签: php

我有这个代码,它已经将mysql导出到csv。

但它也导出表的列名。

我想删除列的名称。

我应该删除或重写哪一部分?

    <?php
    include('connection.php');

    //header to give the order to the browser
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment;filename=exported-books.csv');

    //select table to export the data
    $select_table=mysql_query('select accessionnumber,datereceived,author,booktittle,bookcategory,format,callnumber,isbn,publisher,photo,barcodeid,times,addedby from books');
    $rows = mysql_fetch_assoc($select_table);

    if ($rows)
    {
    getcsv(array_keys($rows));
    }
    while($rows)
    {
    getcsv($rows);
    $rows = mysql_fetch_assoc($select_table);
    }

    // get total number of fields present in the database
    function getcsv($no_of_field_names)
    {
    $separate = '';


    // do the action for all field names as field name
    foreach ($no_of_field_names as $field_name)
    {
    if (preg_match('/\\r|\\n|,|"/', $field_name))
    {
    $field_name = '' . str_replace('', $field_name) . '';
    }
    echo $separate . $field_name;

    //sepearte with the comma
    $separate = ',';
    }

    //make new row and line
    echo "\r\n";
    }
    ?>

TIA

1 个答案:

答案 0 :(得分:0)

$i = 0;

function getcsv($no_of_field_names) {
 $separate = '';

 foreach ($no_of_field_names as $field_name) {
  if($i > 0) {
   if (preg_match('/\\r|\\n|,|"/', $field_name)) {
    $field_name = '' . str_replace('', $field_name) . '';
   }
   echo $separate . $field_name;

   $separate = ',';
  }
  $i++;
 }

 //make new row and line
 echo "\r\n";
}