getColumnMeta不能正确返回字段类型。

4

我有一个使用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);
}

你能展示一下你的 PHP 代码吗? - xdazz
我一定漏掉了什么。有什么问题吗? - sberry
你不是希望unsigned能够成为PHP数据类型的一部分,对吧? - sberry
那么,根据这些信息,我如何知道该列是“INT”类型? - Gajus
2
LONG和INT是相同的。 - Narf
显示剩余2条评论
1个回答

0

我不会使用这个函数来确定列的类型。文档说明它是实验性的,而且这种花式操作并不值得。我会使用:

SHOW COLUMNS FROM [table]

这将在结果中给您纯粹的MySQL类型。它可能不太优雅,但更加可靠。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接