问题
I am Using Following code For getting csv file of mysql database. But I want to skip some fields in database while getting it in csv format.
e.g. Fields In databse are id, firstname, last name, username, password, email, membership date, last login date
While downloading this database through fputcsv option, i doesn't want password, last login date in my csv file.
How to achieve this ?
My current code (which is extracting all fields from database in csv file) is as follows :
<?
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);
}
?>
回答1:
$user_query = mysql_query("SELECT id, firstname, last name, username, email, membership date FROM table ORDER BY RAND()");
If you exclude them from your query - they won't appear in your CSV - simples!
Then just make sure your column names correspond to the field order.
回答2:
<?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;
}
Here is full code for skip fields value or update value also.
来源:https://stackoverflow.com/questions/24123508/how-to-skip-some-fields-from-database-while-using-fputcsv