我有以下方案的 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/