4

I have innodb table with the following scheme, running MySQL 5.5:

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

I am trying to use PDOStatement::getColumnMeta to get id column type, but it gives wrong data:

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 code as requested:

$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);
}
4

1 回答 1

0

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

SHOW COLUMNS FROM [table]

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

于 2014-07-13T03:04:27.570 回答