使用PDO预处理语句和bit
数据类型
使用PDO预处理语句从DB检索结果时,返回空结果集,但如果查询是通过正常的SQL查询完成的,则返回正确的结果
如果省略where子句中的字段is_demo
,它将按预期工作!?
CREATE TABLE IF NOT EXISTS `user` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`client_id` smallint(5) unsigned NOT NULL,
`is_admin` bit(1) NOT NULL,
`is_demo` bit(1) NOT NULL,
`name` varchar(30) NOT NULL,
`pass` varbinary(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `client_id` (`client_id`),
KEY `is_admin` (`is_admin`),
KEY `is_demo` (`is_demo`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;
$sql = "SELECT id,client_id,is_admin,is_demo\n"
."FROM user\n"
."WHERE is_demo=? && client_id=? && name=? && pass=?\n"
."ORDER BY name";
$fields = [
0,
500,
'test',
123
];
$sth = $dbh->prepare($sql);
$sth->execute($fields);
PDOStatement Object
(
[queryString] => SELECT id,client_id,is_admin,is_demo
FROM user
WHERE is_demo=? && client_id=? && name=? && pass=?
ORDER BY name
)
没有返回任何行
SELECT id, client_id, is_admin, is_demo
FROM user
WHERE is_demo=0 && client_id=500 && name='test' && pass=123
ORDER BY name
返回正确的行数
答案 0 :(得分:0)
此处问题\n
无法在查询中
$sql = "SELECT id,client_id,is_admin,is_demo\n"
."FROM user\n"
."WHERE is_demo=? && client_id=? && name=? && pass=?\n"
."ORDER BY name";
答案 1 :(得分:0)
MySQL可以将字符串值“0”转换为在字符串之前用“B”编写代码的位:
$sql = "SELECT id,client_id,is_admin,is_demo\n"
."FROM user\n"
."WHERE is_demo=B? && client_id=? && name=? && pass=?\n"
."ORDER BY name";
$fields = [
'0',
500,
'test',
123
];
$sth = $dbh->prepare($sql);
$sth->execute($fields);