0

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);
 }

 ?>
2
  • Just select the columns you need instead of *. Commented Jun 9, 2014 at 15:28
  • ohhh...yes...Let Me Try...I forgot this...thank you... Commented Jun 9, 2014 at 15:29

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

No problem! Sometimes simple is just too obvious so you miss it!
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;
}

Here is full code for skip fields value or update value also.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.