4

MySQL 5.5 を実行している、次のスキームの innodb テーブルがあります。

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

1 に答える 1

0

列のタイプを判別するためにこの関数を使用することはありません。ドキュメントには、それは実験的なものであり、体操はそれだけの価値がないと述べられています。私は使うだろう:

SHOW COLUMNS FROM [table]

結果に純粋なMySQLタイプが表示されます。少しエレガントではありません。しかし、はるかに信頼性があります。

于 2014-07-13T03:04:27.570 に答える