如何使用 PDO 确定列类型?

发布于 2024-09-08 04:33:20 字数 304 浏览 5 评论 0原文

当使用 PDO 从数据库读取数据时,我需要一种方法来确定数据库列的类型(varchar/numeric/date/...)。

当从数据库中获取值时,PDO 仅生成字符串值,而不管表列的实际类型如何。

是否有任何非驱动程序特定的方法来获取此信息?我知道有一些 SQL 语句可以检索任何给定表的类型,但我更喜欢更通用的解决方案。

编辑: PDOStatement::getColumnMeta() 对我来说没有用,因为我目前使用的 PDO 驱动程序 (Oracle) 不支持它。

I need a way to determine the type of a database column (varchar/numeric/date/...) when reading from the DB with PDO.

When fetching values from the DB, PDO produces only string values, regardless of the actual type of the table column.

Is there any non driver specific way to get this information? I know that there are SQL statements that retrieve the types for any given table but i'd prefer a more generic solution.

EDIT:
PDOStatement::getColumnMeta() is of no use to me, because it's not supported by the PDO driver I use at the moment (Oracle).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

萌酱 2024-09-15 04:33:21

我不久前写了一个函数来提取表列信息。我最终做了这样的事情:

SHOW COLUMNS FROM <table> WHERE Field = ?

对于典型的主键,会产生这样的结果:

+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int(11) | NO   | PRI | NULL    | auto_increment |
+-------+---------+------+-----+---------+----------------+

然后我将输出解析为可用的数组。然而,那是 PHP 5.1.0 之前的版本。现在您可以使用 PDOStatement->getColumnMeta

I wrote a function a while ago which extracted table column information. I ended up doing something like this:

SHOW COLUMNS FROM <table> WHERE Field = ?

For a typical primary key, that produces this:

+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int(11) | NO   | PRI | NULL    | auto_increment |
+-------+---------+------+-----+---------+----------------+

I then parsed the output into a usable array. However, that was pre-PHP 5.1.0. Now you can probably use PDOStatement->getColumnMeta.

香橙ぽ 2024-09-15 04:33:21

如果您使用 Oracle:

select COLUMN_NAME,
       DATA_TYPE,
       DATA_LENGTH,
       DATA_PRECISION,
       DATA_SCALE
from user_tab_cols
where table_name = '<Table Name>'
order by column_id

但它不可移植

许多 SQL 风格也有

DESCRIBE <Table Name>

If you're working with Oracle:

select COLUMN_NAME,
       DATA_TYPE,
       DATA_LENGTH,
       DATA_PRECISION,
       DATA_SCALE
from user_tab_cols
where table_name = '<Table Name>'
order by column_id

but it isn't portable

Many SQL flavours also have

DESCRIBE <Table Name>
哥,最终变帅啦 2024-09-15 04:33:21

如果您使用 Postgres:

select
    CHARACTER_MAXIMUM_LENGTH,
    COLUMN_NAME,
    IS_NULLABLE,
    COLUMN_DEFAULT,
    NUMERIC_PRECISION,
    NUMERIC_SCALE,
    UDT_NAME 
from
    INFORMATION_SCHEMA.COLUMNS 
where
    TABLE_NAME='table_name'

If you're working with Postgres:

select
    CHARACTER_MAXIMUM_LENGTH,
    COLUMN_NAME,
    IS_NULLABLE,
    COLUMN_DEFAULT,
    NUMERIC_PRECISION,
    NUMERIC_SCALE,
    UDT_NAME 
from
    INFORMATION_SCHEMA.COLUMNS 
where
    TABLE_NAME='table_name'
听不够的曲调 2024-09-15 04:33:20

看看这个方法: PDOStatement->getColumnMeta

Take a look at this method: PDOStatement->getColumnMeta

信愁 2024-09-15 04:33:20

这就是我在 WraPDO 类中的做法:

$tomet = $sth->getColumnMeta($column_index);
$tomet['type'] = $this->_translateNativeType($tomet['native_type']);

private function _translateNativeType($orig) {
    $trans = array(
        'VAR_STRING' => 'string',
        'STRING' => 'string',
        'BLOB' => 'blob',
        'LONGLONG' => 'int',
        'LONG' => 'int',
        'SHORT' => 'int',
        'DATETIME' => 'datetime',
        'DATE' => 'date',
        'DOUBLE' => 'real',
        'TIMESTAMP' => 'timestamp'
    );
    return $trans[$orig];
}

$sth: PDOStatement->获取ColumnMeta

This is how I did it in my WraPDO class:

$tomet = $sth->getColumnMeta($column_index);
$tomet['type'] = $this->_translateNativeType($tomet['native_type']);

private function _translateNativeType($orig) {
    $trans = array(
        'VAR_STRING' => 'string',
        'STRING' => 'string',
        'BLOB' => 'blob',
        'LONGLONG' => 'int',
        'LONG' => 'int',
        'SHORT' => 'int',
        'DATETIME' => 'datetime',
        'DATE' => 'date',
        'DOUBLE' => 'real',
        'TIMESTAMP' => 'timestamp'
    );
    return $trans[$orig];
}

$sth: PDOStatement->getColumnMeta

紫罗兰の梦幻 2024-09-15 04:33:20

它被标记为“实验性”,但是 PDOStatement->getColumnMeta< /code>方法看起来会做你想做的事。

It's marked as "experimental", but the PDOStatement->getColumnMeta method looks like it will do what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文