【问题标题】:How to get the original table/column name when using PDO?使用 PDO 时如何获取原始表/列名?
【发布时间】:2012-01-15 01:46:40
【问题描述】:

使用 PHP 的 mysqli 扩展,我可以使用 fetch_field() 方法通过 orgnameorgtable 在结果中获取列和表的原始(非别名)名称。 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
)

【问题讨论】:

  • 根据文档,调用PDOStatement.getColumnMeta 的结果包括一个“表”列,其中包含“数据库返回的该列的表的名称”。这不是你在实践中看到的吗?
  • 我将结果添加到我的帖子中!
  • @d.hill 你找到解决办法了吗?

标签: php pdo


【解决方案1】:

你可以使用fetch方法

    while ($row = $stmt->fetch()) {
        var_dump($row);
    }

【讨论】:

    猜你喜欢
    • 2021-06-05
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 2014-12-07
    • 1970-01-01
    相关资源
    最近更新 更多