简体   繁体   English

如何从PHP或Postgresql中的当前查询获取表名

[英]How to get table name from current query in php or postgresql

is it possible to get table name from current query using pdo php or normal sql in postgresql? 是否有可能使用pdo php或postgresql中的普通sql从当前查询中获取表名?

In pdo php a function exists like PDOStatement::getColumnMeta but this function doesn't work well, because it return name of alias in query and when I use notation like 在pdo php中,存在像PDOStatement::getColumnMeta这样的函数,但是该函数不能很好地工作,因为它在查询中以及当我使用诸如

SELECT configuration.id AS other_name
FROM   configuration.mytable AS other_table_name
INNER JOIN  configuration.some_table AS other_some_table_name
ON other_table_name.id = other_some_table_name.id

it back strange values. 它返回奇怪的值。 I`m interested to get base name of table without aliases. 我有兴趣获取不带别名的表的基本名称。 Anybody know better way to get table name of actual query? 有人知道更好的方法来获取实际查询的表名吗?

Update for Elvis Ciotii answer Elvis Ciotii答案的更新

Here is sql: 这是sql:

    SELECT        *
    FROM           configuration.applications
    WHERE        url=:url

Here is result 这是结果

      Array
            (
                [pgsql:oid] => 23
                [native_type] => int4
                [name] => id
                [len] => 4
                [precision] => -1
                [pdo_type] => 1
            )

in documentation php.net function PDOStatement::getColumnMeta() return an associative array containing the following values representing the metadata for a single column: 在文档php.net中,函数PDOStatement :: getColumnMeta()返回一个关联数组,该数组包含以下表示单个列的元数据的值:

native_type, driver:decl_type, flags, name, table, len, precision, pdo_type native_type,驱动程序:decl_type,标志,名称,表,len,精度,pdo_type

Where is in my result flags and table key?? 我的结果标志和表键在哪里?

I need this value because i`m working at my little framework where i want implements to model class special returnig function with resulting executed queries. 我需要这个值,因为我正在我的小框架上工作,在这里我想要实现为类特殊的returnig函数建模并生成执行的查询。 I was thinking if i cant get this value from pdo i cant get this value in normal query. 我在想如果我不能从pdo获得此值,我无法在普通查询中获得此值。 etc. SHOW TABLES or something like this. 等。显示表或类似的东西。

if you compose the query, you already have the table name, why do you want it to parse from the query ? 如果组成查询,您已经有了表名,为什么要从查询中解析它呢?

anyway, to answer to your question PDOStatement::getColumnMeta() actuall returns the table information 无论如何,要回答您的问题PDOStatement::getColumnMeta()实际上返回table信息

http://www.php.net/manual/en/pdostatement.getcolumnmeta.php http://www.php.net/manual/zh/pdostatement.getcolumnmeta.php

what strange values do you get instead ? 您会得到什么奇怪的值?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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