[英]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. 我尝试仅选择某些
column_names
(而不是column data)设置为CSV文件的标题。
Right now, I can only pull all of the column names with $result = mysql_query("SHOW COLUMNS FROM ".$table);
现在,我只能使用
$result = mysql_query("SHOW COLUMNS FROM ".$table);
拉出所有列名$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? 例如,如何选择或仅显示我在
$columns
列出的列的列名?
EDIT - I'm adding my full PHP here to provide more context. 编辑 -我在这里添加完整的PHP以提供更多上下文。 Line 7 is my problem.
第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;
?>
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);
如果仍要访问从查询中检索到的内容,则可以
$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: 如果要打印CSV文件,请执行以下操作:
$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. 故意重复foreach,因此此示例更加清楚。
You can query the USER_TAB_COLUMNS table for table column metadata. 您可以查询USER_TAB_COLUMNS表以获取表列元数据。
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... 因此您可能正在使用SQL Server-对于SQL Server,请使用此...
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 类型='V'用于视图类型='U'用于表
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].'",';
然后稍后使用
$csv_output .= '"'.$rowr[$j].'",';
That worked perfectly without redoing my entire code. 在不重做我的整个代码的情况下,它可以完美地工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.