PDO在准备语句中不能使用整数来比较mysql ENUM [英] PDO cannot compare mysql ENUM using integers in prepared statements
问题描述
我正在使用PDO和准备的语句,但是当将ENUM字段与整数进行比较时,我似乎无法获得任何结果。
I am using PDO and prepared statements, but i cannot seem to get any results when comparing an ENUM field with an integer.
示例:
$db = new PDO('mysql:host=localhost;dbname=****', '***', '***');
$s = $db->prepare('SELECT id FROM t2 WHERE lang = ?');
$s->execute(array('en')); // Works
print_r($s->fetchAll());
$s->execute(array(2)); // Does not work
print_r($s->fetchAll());
我正在测试此表:
DROP TABLE IF EXISTS t2;
CREATE TABLE t2 (
id int(10) NOT NULL AUTO_INCREMENT,
lang enum('no','en','fr') NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO t2 (id, lang) VALUES (NULL , 'en');
有关如何让这项工作有任何想法?
Any idea on how to get this to work?
我正在转换为PDO,我不想在我的应用程序中重写所有的常量,枚举和查询:(
I am converting to PDO, and I'd prefer not to rewrite all constants, enums, and queries in my app :(
推荐答案
2
不是一个有效的ENUM元素,很简单。
2
is not a valid ENUM element. It's as simple as that.
在原始MySQL中工作,当您执行 lang = 2
时,它是暴露了列表的底层存储机制(基本上它只是一个标准化的值,但标准化是从你隐藏的通过ENUM列)
The reason it works in raw MySQL when you do lang = 2
, is that it's exposing the underlying storage mechanism for the list (basically it's just a normalized value, but the normalization is hid from you by the ENUM column).
我建议不要这样做,MySQL隐藏了一个原因的实现,意识到使用 2
进行比较只不过是一个魔术数字。 ..
I'd suggest not trying to do this. MySQL hides the implementation for a reason. Realize that using 2
for the comparison is nothing more than a magic number...
这篇关于PDO在准备语句中不能使用整数来比较mysql ENUM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!