在這一個上空了,可以使用一些見解。MYSQL到CSV - 如何匹配列標題與選定的列
我試着只選擇某些column_names
(不是列數據)設置爲CSV文件的標題。
現在,我只能把所有的列名與 $result = mysql_query("SHOW COLUMNS FROM ".$table);
的問題是,它拉的所有列名的,我只希望某些列的數據。要獲取的數據值這個查詢工作完美:
$values = mysql_query("SELECT ".$columns." FROM ".$table." WHERE channel_id=26");
如何選擇或只顯示我在$columns
列出列的列名,例如?
編輯 - 我在這裏添加我的完整PHP以提供更多上下文。第7行是我的問題。
<?php
$table = 'exp_channel_data'; // table we want to export
$columns = 'entry_id, field_id_26, field_id_27, '; // only the columns we want to show
$file = 'registrations-from-web'; // csv name.
$result = mysql_query("SHOW COLUMNS FROM ".$table);
$count = mysql_num_rows($result);
$csv_output = "";
if ($count > 0)
{
while ($row = mysql_fetch_assoc($result))
{
$csv_output .= $row['Field'].", ";
}
}
$csv_output .= "\n";
$values = mysql_query("SELECT ".$columns." FROM ".$table." WHERE channel_id=26");
while ($rowr = mysql_fetch_row($values))
{
for ($j=0; $j<$count; $j++)
{
$csv_output .= $rowr[$j].", ";
}
$csv_output .= "\n";
}
$filename = $file."_".date("d-m-Y_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");
print $csv_output;
exit;
?>
做'$列=「 column1_i_want,column2_i_want,column3_i_want「'? – Jasper
我有,但我怎麼做它的列名?什麼是適當的查詢?沒有什麼我嘗試工作。 –