我正在尝试添加一个函数,该函数将创建一个 csv 以从数据库中的某个表下载。我已经让它部分工作,但无法弄清楚如何从 csv 的行部分中排除某些列。到目前为止,这是我的代码,
$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;
我已经从 $result 输出中排除了列 invhead_id 和 note,但也需要从 $values 输出中排除它们,但是这是我无法开始工作的地方。谁能指出我正确的方向?
谢谢