更改 mysql 列名称以与 FPutCSV() 一起使用
$result = mysql_query("show columns from mash");
for ($i = 0; $i < mysql_num_rows($result); $i++) {
$colArray[$i] = mysql_fetch_assoc($result);
$fieldArray[$i] = $colArray[$i]['Field'];
}
fputcsv($fp,$fieldArray);
获取 mysql 列名称,然后将它们输出到创建的 CSV 的顶部。
但是,我知道列名称是什么,如何更改它们以用于 CSV 中的输出?
IE。如果显示列输出:id,field1,field2,field3,我如何制作这些ID,Field 1,Field 2,Field 3。不仅要大写,还要选择它们将是什么...
$result = mysql_query("show columns from mash");
for ($i = 0; $i < mysql_num_rows($result); $i++) {
$colArray[$i] = mysql_fetch_assoc($result);
$fieldArray[$i] = $colArray[$i]['Field'];
}
fputcsv($fp,$fieldArray);
to grab mysql column names and then output them at the top of the created CSV.
However, i know what the column names are going to be, how can i change them for the output in the CSV?
Ie. if the show columns output: id, field1, field2, field3, how can i make these ID, Field 1, Field 2, Field 3. Not just capitalise, but choose what they are going to be...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须拥有一个列到标签的列表,这将使脚本特定于数据库:
或者,您必须使用此类元数据创建一个数据库。它可以具有三个字段:source_table、source_column、column_label。这增加了另一个查询,但允许代码变得通用。
最后一种方法是使用一些带有分隔符的简单命名约定,例如下划线 (_),然后删除下划线并应用标题大小写。 field_1 变为“字段 1”,“user_id”变为“用户 ID”,依此类推。
You'd have to either have a list of columns-to-labels, which would make the script specific to the database:
OR, you'd have to make a database with this sort of meta data. It could have three fields: source_table, source_column, column_label. That adds another query to the mix, but would allow the code to be made generic.
The last route would be to use some simple naming convention with a separator, like underscore (_), then you remove the underscores and apply title case. field_1 becomes "Field 1", "user_id" becomes "User Id", and so on.