PDOStatement::getColumnMeta

(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.2.0)

PDOStatement::getColumnMeta 返回结果集中一列的元数据

说明

PDOStatement::getColumnMeta ( int $column ) : array
Warning

此函数是实验性的。此函数的表象,包括名称及其相关文档都可能在未来的 PHP 发布版本中未通知就被修改。使用本函数风险自担。

检索一个在结果集中以0开始索引的列的元数据作为一个关联数组。

Warning

并非所有 PDO 驱动都支持 PDOStatement::getColumnMeta()

参数

column

结果集中以0开始索引的列。

返回值

返回一个关联数组,它包含了下列表示一个单独列的元数据的值:

列的元数据
名称
native_type 用于表示列值的 PHP 原生类型。
driver:decl_type 在数据库中用于表示列值的 SQL 类型。如果结果集中的列是一个函数的结果,则该值不能被 PDOStatement::getColumnMeta() 返回。
flags 任何设置于此列的标记。
name 通过数据库返回的列名。
table 通过数据库返回的该列的表名
len 该列的长度。除浮点小数外通常为 -1
precision 该列的数值精度。除浮点小数外通常为 0
pdo_type PDO::PARAM_* 常量为代表的列类型。

如果结果集不存在,或者是请求的列在结果集中不存在,则返回 false

更新日志

版本 说明
5.2.3 table 字段

范例

Example #1 检索列的元数据

下面例子展示了在一个PDO_SQLITE中,检索一个通过函数(COUNT)生成单独列的元数据的结果。

<?php
$select 
$DB->query('SELECT COUNT(*) FROM fruit');
$meta $select->getColumnMeta(0);
var_dump($meta);
?>

以上例程会输出:

array(6) {
  ["native_type"]=>
  string(7) "integer"
  ["flags"]=>
  array(0) {
  }
  ["name"]=>
  string(8) "COUNT(*)"
  ["len"]=>
  int(-1)
  ["precision"]=>
  int(0)
  ["pdo_type"]=>
  int(2)
}

参见

User Contributed Notes

jcastromail at yahoo dot es 10-Mar-2020 08:49
This method is supported by sql server too (at least the version of the PDO driver 5.7.1)

Example:

array(8) {
  ["flags"]=>
  int(0)
  ["sqlsrv:decl_type"]=>
  string(12) "int identity"
  ["native_type"]=>
  string(6) "string"
  ["table"]=>
  string(0) ""
  ["pdo_type"]=>
  int(2)
  ["name"]=>
  string(8) "IdCompra"
  ["len"]=>
  int(10)
  ["precision"]=>
  int(0)
}
frankno dot 94 at gmail dot com 16-Aug-2019 09:32
I recently added a note to a small function that went through the results of PDOStatement::fetch and mapped columns to integer or float types.

I forgot to test for columns that can have a null value!

I'm updating the code here, sorry.

This website doesn't let me edit the original comment, so I'm just gonna link to a gist in case it needs even further fixes.

https://gist.github.com/AeonFr/db3b2a8a95753076d9e4da42d29365d2

I basically added this code:

<?php
if ($value === null) {
   continue;
}
?>
frankno dot 94 at gmail dot com 13-Aug-2019 10:05
I was able to successfully pull off basic type conversion for integer and float values in PDO.

NULL values were returning as null, but integers and floats weren't, so I decided to go with a simple solution.

It's not tested on all database types, so if you think it needs improvement, let me know (my twitter is "francanobr")

<?php

// Returns the contents of $stm (PDO Statement)
// as an associative array, with correct native types
// for integers, nulls, and floats

$array = [];

while (
$fila = $stm->fetch(\PDO::FETCH_ASSOC)) {

   
$i = 0;
    foreach (
$fila as $key => $value) {
       
$columnMeta = $stm->getColumnMeta($i);

        switch (
$columnMeta['native_type']) {
            case
'LONG':
            case
'TINY':
               
$fila[$key] = intval($value);
                break;
            case
'DOUBLE':
               
$fila[$key] = floatval($value);
                break;
        }

       
$i++;
    }

   
$array[] = $fila;
}

return
$array;
?>

The code sample is part of a function, the function declaration is not included, evaluate how to integrate it with your app.
Rodrigo Silva 03-Aug-2019 11:57
native_type has the following mapping for MySQL:
(tested in PHP 5.4, MySQL 5.7)

INT(11) (PKs) => LONG
TINYINT(4)    => TINY
DOUBLE        => DOUBLE
VARCHAR       => VAR_STRING
CHAR          => STRING
DATE          => DATE
Functions     => VAR_STRING
- DATEFORMAT()
- CONCAT()
colin at fusionbox dot com 23-Jan-2009 10:38
This method is supported in the MySQL 5.0+ driver.  It can be used for object hydration:

<?php

$pdo_stmt
= $dbh->execute('SELECT discussion.id, discussion.text, comment.id, comment.text FROM discussions LEFT JOIN comments ON comment.discussion_id = discussion.id');

foreach(
range(0, $pdo_stmt->columnCount() - 1) as $column_index)
{
 
$meta[] = $pdo_stmt->getColumnMeta($column_index);
}

while(
$row = $pdo_stmt->fetch(PDO::FETCH_NUM))
{
  foreach(
$row as $column_index => $column_value)
  {
   
//do something with the data, using the ids to establish the discussion.has_many(comments) relationship.
 
}
}

?>

If you are building an ORM, this method is very useful to support more natural SQL syntax.  Most ORMs require the column names to be aliases so that they can be parsed and turned into objects that properly represent has_one, has_many, many_to_many relationships.
asohn aircanopy net 29-Feb-2008 09:21
I found a short discussion related to this function at
http://www.sitepoint.com/forums/showthread.php?t=497257

I haven't tried it (yet?) but hopefully someone can find it useful.