使用PDO时如何获取原始表/列名?
借助 PHP 的 mysqli 扩展,我可以使用 fetch_field() 方法通过 orgname 和 orgtable 获取列和表的原始(无别名)名称结果中的代码>。 PDO 提供了 getColumnMeta() 方法,但不提供有关原始表名和列名的信息;它只返回别名。
有没有其他方法可以通过 PDO 获取此信息?我一直在考虑解析 SQL 查询中的信息,但我希望有更漂亮的解决方案...
SELECT id AS col1, session AS col2 FROM sessions AS table1;
使用 PDOStatement::getColumnMeta()
的结果:
Array
(
[native_type] => LONG
[flags] => Array
(
[0] => not_null
[1] => primary_key
)
[table] => table1
[name] => col1
[len] => 10
[precision] => 0
[pdo_type] => 2
)
Array
(
[native_type] => VAR_STRING
[flags] => Array
(
[0] => not_null
[1] => unique_key
)
[table] => table1
[name] => col2
[len] => 32
[precision] => 0
[pdo_type] => 2
)
With the mysqli extension for PHP, I could use the fetch_field()
method to get the original (unaliased) names for columns and tables via orgname
and orgtable
in the result. PDO provides the method getColumnMeta()
, but offers no information about the original table and column names; it only returns aliases.
Are there any alternatives to get this information with PDO? I've been thinking about parsing the information from the SQL-query, but I hope there are prettier solutions...
SELECT id AS col1, session AS col2 FROM sessions AS table1;
Results using PDOStatement::getColumnMeta()
:
Array
(
[native_type] => LONG
[flags] => Array
(
[0] => not_null
[1] => primary_key
)
[table] => table1
[name] => col1
[len] => 10
[precision] => 0
[pdo_type] => 2
)
Array
(
[native_type] => VAR_STRING
[flags] => Array
(
[0] => not_null
[1] => unique_key
)
[table] => table1
[name] => col2
[len] => 32
[precision] => 0
[pdo_type] => 2
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 fetch 方法
You can use fetch method