【问题标题】:Is there anyway to tell the datatype in a postgres array?无论如何要告诉 postgres 数组中的数据类型?
【发布时间】:2015-11-02 17:00:57
【问题描述】:

我正在使用 Postgres 9.4 数据库,并使用 PHP 作为我的前端。

我可能运行的一般查询如下所示:

PHP:

$query = "select * from some_table";
pg_prepare($connection,"some_query",$query);
$result = pg_execute($connection,"some_query",array());

while ($row = pg_fetch_array($result,null,PGSQL_ASSOC)) {
    echo $row['some_field'];
    echo $row['some_field_1'];
    echo $row['some_field_2'];
}

我遇到了一个前端,它需要知道吐出的列的数据类型 - 特别是我需要知道 echo 的数据库字段何时是 timestamp 列。

显然我可以分辨出integersstring,但是时间戳有点不同。

我想我可以看看 strtotime() 是否返回 false,但这对我来说似乎有点脏。

所以我的问题是:

是否有 PHP 内置函数可以返回数据库行的多维数组,不仅包含 $key=>$value 对,还包含 datatype

对此的任何帮助将不胜感激 - 谢谢!

【问题讨论】:

标签: php arrays postgresql sqldatatypes


【解决方案1】:

您可以像任何其他查询一样从information_schema.columns 查询并获取:

SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name='some_table'

或者在查询后使用pg_field_type():

$type = pg_field_type($result, 0);

但是您需要知道列在结果中的位置,因此您应该(无论如何都是最佳做法)列出列。对于上述情况,使用0 将在下面的查询中给出col1 的类型::

SELECT col1, col2, col3 FROM some_table

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    相关资源
    最近更新 更多