Forum Moderators: coopster

Message Too Old, No Replies

Export Fields from DB

Export fields from db to CSV for download

         

TheLazyAce

1:11 pm on Jan 31, 2011 (gmt 0)

10+ Year Member



I have a SQL DB that contains info about the club members. I have two issues:

1st issue when the code runs It echo out the data in the browser. I want it to the file to upload to the users computer requesting it.

2nd issue- The table contains more info then is needed. All that I want provided is the names, address, contact into, etc... Can someone point me the right to write this part of code. The code I have been working with is listed below. This was a function that I have been modifying this code, the original code is not mine.

<?php

/**
*
*
* @version $Id$
* @copyright 2011
*/

function exportMysqlToCsv($table,$filename = 'Members.csv')
{
$csv_terminated = "\n";
$csv_separator = ",";
$csv_enclosed = '"';
$csv_escaped = "\\";
$sql_query = "select * from $table";

// Gets the data from the database
$result = mysql_query($sql_query);
$fields_cnt = mysql_num_fields($result);


$schema_insert = '';

for ($i = 0; $i < $fields_cnt; $i++)
{
$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
$schema_insert .= $l;
$schema_insert .= $csv_separator;
} // end for

$out = trim(substr($schema_insert, 0, -1));
$out .= $csv_terminated;

// Format the data
while ($row = mysql_fetch_array($result))
{
$schema_insert = '';
for ($j = 0; $j < $fields_cnt; $j++)
{
if ($row[$j] == '0' || $row[$j] != '')
{

if ($csv_enclosed == '')
{
$schema_insert .= $row[$j];
} else
{
$schema_insert .= $csv_enclosed .
str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
}
} else
{
$schema_insert .= '';
}

if ($j < $fields_cnt - 1)
{
$schema_insert .= $csv_separator;
}
} // end for

$out .= $schema_insert;
$out .= $csv_terminated;
} // end while

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($out));
// Output to browser with appropriate mime type, you choose ;)
header("Content-type: text/x-csv");
//header("Content-type: text/csv");
//header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$filename");
echo $out;
exit;

}


?>

TheLazyAce

1:36 am on Feb 1, 2011 (gmt 0)

10+ Year Member



I was able to finally get the code to work. I still need advice on filtering the fields, of example some of the fields are username, password, boardmember, and about 8 or 9 more fields.

TheLazyAce

1:40 am on Feb 1, 2011 (gmt 0)

10+ Year Member



I was able to finally get the code to work. I still need advice on filtering the fields, for example some of the fields are username, password, boardmember, and about 8 or 9 more fields it don't want. (sorry I should have proofed)

omoutop

11:00 am on Feb 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Change this
$sql_query = "select * from $table";

to this

$sql_query = "select field1, field2, field3 from $table";

where field1, field2, field3 the name of the table fields you wish to export

TheLazyAce

1:34 pm on Feb 1, 2011 (gmt 0)

10+ Year Member



Thank you, this will save time. I have been studying the script and thinking about different ways. Great I will do so, again thank you.

Ed

TheLazyAce

1:11 am on Feb 2, 2011 (gmt 0)

10+ Year Member



omoutop, I just want to thank you again. The direction I was going was re-inventing the wheel. I have been working with PHP and SQL now for about 8 months, and you just turned on one of the Christmas Tree lights on this 100 foot strand. The problem is I still need to get about another 80 foot of them working. Again thank you, this one item taught me alot.

Ed

omoutop

7:01 am on Feb 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I am glad I could help.

As many will agree here, its best practice not to use "Select * from".
Instead type the fields one by one. Although it seems a lot of work, in the long run it proves its usefulness.