简体   繁体   中英

MYSQL to CSV - How to match column headings with selected columns

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;

?>

If you know the name of the columns, you can add them and/or print them directly from an array. If you want to have access to the ones retrieved from the query anyway, you can $result = mysql_fetch_assoc($values); per row, and the keys will always contain the name of the columns since the resulting array is associative.

Try:

$values = mysql_query('SELECT '. $columns. ' FROM '.
    $table. ' WHERE channel_id = 26');
print_r(mysql_fetch_assoc($values));

For an insight of the contents of the resulting array.

Now try:

$first = 1;
echo '<table>';
while ($assoc = mysql_fetch_assoc($values))
{
    echo '<tr>';
    if ($first)
    {   
        foreach ($assoc as $key => $value)
            echo "<th>$key</th>\n";
        $first = 0;
    }   
        else
    {   
        foreach ($assoc as $key => $value)
            echo "<td>$value</td>\n";
    }   
    echo '</tr>';
}
echo '</table>';

If what you want is to print a CSV file:

$columns = Array();
$values = Array();
$first = 1;
for ($i = 0; $assoc = mysql_fetch_assoc($values); ++$i)
{
    if ($first)
    {   
        foreach ($assoc as $key => $value)
            $columns[] = $key;
        $first = 0;
    }   
        else
    {   
        foreach ($assoc as $key => $value)
            $values[$i][] = $value;
    }   
}
$out = fopen('php://output', 'w');
fputcsv($out, $columns);
foreach ($values as $line)
    fputcsv($out, $line);

The foreach is repeated on purpose so this example is more clear.

You can query the USER_TAB_COLUMNS table for table column metadata.

SELECT table_name, column_name, data_type, data_length
FROM USER_TAB_COLUMNS
WHERE table_name = 'yourTableName'

so you might be using SQL server - for SQL server use this...

SELECT [name] AS [Column Name]
FROM syscolumns
WHERE id = (SELECT id FROM sysobjects WHERE type = 'V' AND [Name] = 'Your table name')

Type = 'V' for views Type = 'U' for tables

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM