0

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

例如,数据库中的字段是 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);
 }

 ?>
4

2 回答 2

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

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

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

于 2014-06-09T15:30:39.810 回答
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;
}

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

于 2016-10-05T09:39:06.040 回答