如何在使用fputcsv时从数据库中跳过某些字段

时间:2014-06-09 15:26:50

标签: php mysql csv fputcsv

我正在使用以下代码获取mysql数据库的csv文件。但我希望跳过数据库中的某些字段,同时以csv格式获取它。

e.g。数据库中的字段是id,名字,姓氏,用户名,密码,电子邮件,会员日期,上次登录日期

通过fputcsv选项下载此数据库时,我不想在我的csv文件中输入密码,上次登录日期。

如何实现这一目标?

我当前的代码(从csv文件中的数据库中提取所有字段)如下:

<?
 require_once("../db.php");

 $contents="Database Id,Last Name,First Name,User Name,Email,Permanent Address,Communication Address,Mobile,Birth Date,Gender,Payment Mode,Form Submission Date,Registration Activation Date,Memebrship Expires On,Installment\n";

 $user_query = mysql_query("SELECT * from table ORDER BY RAND()");
 $contents = strip_tags($contents);

 header("Content-Disposition: attachment; filename=my_members_".date('d-F-Y').".csv");
 $out = fopen('php://output', 'w');
 fputcsv($out, array('Database Id', 'Last Name', 'First Name' , 'User Name' , 'Email' , 'Permanent Address', 'Communication Address', 'Mobile', 'Birth Date', 'Gender', 'Payment Mode', 'Form Submission Date', 'Registration Activation Date', 'Membership Expires On', 'Installment'));

 while ($row = mysql_fetch_assoc($user_query)) {
 fputcsv($out, $row);
 }

 ?>

2 个答案:

答案 0 :(得分:1)

 $user_query = mysql_query("SELECT id, firstname, last name, username, email, membership date FROM table ORDER BY RAND()");

如果您将其从查询中排除 - 它们不会出现在您的CSV中 - 简单!

然后只需确保您的列名与字段顺序相对应。

答案 1 :(得分:0)

    <?php
$db_con = mysql_connect("localhost","root","");
$db_sel = mysql_select_db('test_one');
$result = mysql_query('SELECT * FROM `tbl_test`');


if (!$result) die('Couldn\'t fetch records');
$num_fields = mysql_num_fields($result);
$headers = array();
for ($i = 0; $i < $num_fields; $i++) 
{
    $headers[] = mysql_field_name($result , $i);
}
$NewFName = 'sample_csv_'.rand(1,2).'.csv';
copy('sample.csv',$NewFName);
/*
Set permision to file
*/
chmod($NewFName, 0777);

$fp = fopen($NewFName, 'a+');

if ($fp && $result) 
{
        $ValArr = array();
    $count = 0;

   while ($row = mysql_fetch_row($result)) 
   {


    for($t=0; $t<count(array_values($row)); $t++)
    {
        $count = $t;
        /*
        Update value for particular column 
        */
        if($count == 7 || $count == 8 || $count == 9 || $count == 10 || $count == 11 || $count == 12 || $count == 13 || $count == 14 || $count == 15 || $count == 16 || $count == 17 || $count == 18 || $count == 19 || $count == 20 || $count == 21 || $count == 32 || $count == 35 || $count == 37 || $count == 40)
        {
            if(array_values($row)[$t] == '0')
            {
                $NewVal = 'No';
            }else
            if(array_values($row)[$t] == '1')
            {
                $NewVal = 'Yes';
            }else
            {

            }
        }else
        {
            $NewVal = array_values($row)[$t];
        }
        /*
        Skip some database fields value to write in CSV
        */

        if($count == 34 || $count == 39 || $count == 44 || $count == 45 || $count == 46 || $count == 47 || $count == 48)
        {

        }else
        {
            $NewVal = str_replace(',','&',$NewVal);
            fwrite($fp, $NewVal.',');           

        }

    }
    fwrite($fp, "\n");          
}



 fputcsv($fp, $ValArr);
   die;
}

以下是跳过字段值或更新值的完整代码。