Page 1 of 1

Exclude DB Columns

Posted: Thu Jul 05, 2012 3:20 pm
by clj83
Hi.

I am attempting to add a function that will create a csv for download from a certain table in the db. I have got it partially working but cannot figure out how to exclude certain columns from the rows section of the csv. Here is my code so far,

Code: Select all

$host = 'xxx';
$user = 'xxxx';
$pass = 'xxxx';
$db = 'xxxx';
$table = 'headers';
$file = 'export';

$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

$result = mysql_query("SHOW COLUMNS FROM ".$table." WHERE Field NOT IN ('invhead_id', 'note');");
$i = 0;
if (mysql_num_rows($result) > 0) 
{
   while ($row = mysql_fetch_assoc($result)) 
   {
      $csv_output .= $row['Field'].", ";
      $i++;
   }
}
$csv_output .= "\n";

$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) 
{
   for ($j=0;$j<$i;$j++) 
   {
      $csv_output .= $rowr[$j].", ";
   }
   
   $csv_output .= "\n";
}


print $csv_output;
exit;
What I would like to do is also exclude the columns invhead_id and note from the rows output but so far all things I have tried have failed. Can anyone point me in the right direction?

Thanks

Re: Exclude DB Columns

Posted: Thu Jul 05, 2012 3:42 pm
by calguy1000
This is a general programming question, not related to CMSMS.