Coming up empty on this one and could use some insight.
I’m try to select only certain column_names (not column data) to set as a header for CSV file.
Right now, I can only pull all of the column names with
$result = mysql_query("SHOW COLUMNS FROM ".$table);
The problem is that it pulls all of the column names and I’m only wanting certain columns’ data. To get the data values this query is working perfectly:
$values = mysql_query("SELECT ".$columns." FROM ".$table." WHERE channel_id=26");
How do I select or show only the column names for the columns I list out in $columns, for example?
EDIT – I’m adding my full PHP here to provide more context. Line 7 is my problem.
<?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;
?>
I don’t know why I was missing this, but since I’m already having to list out the columns I need, I just needed to turn that string into an array to make is it work.
$column_names = Array($columns);then later use
$csv_output .= '"'.$rowr[$j].'",';That worked perfectly without redoing my entire code.