php - getColumnMeta 不返回正确的字段类型

标签 php pdo

我有以下方案的 innodb 表,运行 MySQL 5.5:

`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`category_id` int(10) unsigned DEFAULT NULL,
`uuid` varchar(36) NOT NULL,

我正在尝试使用 PDOStatement::getColumnMeta获取 id 列类型,但它给出了错误的数据:

array(3) {
  [0]=>
  array(7) {
    ["native_type"]=>
    string(4) "LONG"
    ["flags"]=>
    array(2) {
      [0]=>
      string(8) "not_null"
      [1]=>
      string(11) "primary_key"
    }
    ["table"]=>
    string(7) "entries"
    ["name"]=>
    string(2) "id"
    ["len"]=>
    int(10)
    ["precision"]=>
    int(0)
    ["pdo_type"]=>
    int(2)
  }
  [1]=>
  array(7) {
    ["native_type"]=>
    string(4) "LONG"
    ["flags"]=>
    array(1) {
      [0]=>
      string(12) "multiple_key"
    }
    ["table"]=>
    string(7) "entries"
    ["name"]=>
    string(11) "category_id"
    ["len"]=>
    int(10)
    ["precision"]=>
    int(0)
    ["pdo_type"]=>
    int(2)
  }
  [2]=>
  array(7) {
    ["native_type"]=>
    string(10) "VAR_STRING"
    ["flags"]=>
    array(1) {
      [0]=>
      string(8) "not_null"
    }
    ["table"]=>
    string(7) "entries"
    ["name"]=>
    string(4) "uuid"
    ["len"]=>
    int(108)
    ["precision"]=>
    int(0)
    ["pdo_type"]=>
    int(2)
  }
}

请求的 PHP 代码:

$select = $db
    ->query("
    SELECT
        `id`,
        `category_id`,
        `uuid`
    FROM
        `entries`
    LIMIT 1;
    ;
    ");


$meta   = [];

for($i = 0, $j = $select->columnCount(); $i < $j; $i++)
{
    $meta[] = $select->getColumnMeta($i);
}

最佳答案

我不会使用此函数来确定列的类型。该文档指出它是实验性的,体操只是不值得。我会使用:

SHOW COLUMNS FROM [table]

这将在结果中为您提供纯 MySQL 类型。它有点不那么优雅。但更可靠。

关于php - getColumnMeta 不返回正确的字段类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11356842/

相关文章:

php - gettext() 中的多个复数形式

php - 带有 bindParam 的 PDO 语句中的奇怪行为

php - php 可以知道用户的时间和日期吗?

javascript - 去除引号以外的所有空格

javascript - 如何使用 PHP/JS 计算下拉菜单的值?

php - 忽略 MySQL PDO PHP 命令行中的 1 行不起作用

php - PDO 为 mysql_num_rows 准备了等效语句

PHP - PDO 连接类 - 无法访问

PHP无限滚动拉随机结果?

PHP多行,连接字符串