PDO准备了带有位域的语句

使用PDO预处理语句和bit数据类型时遇到问题

当使用PDO准备语句从数据库检索结果时,会返回一个空结果集,但如果查询是通过正常的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 ;

PDO准备声明

$sql = "SELECT id,client_id,is_admin,is_demon"
    ."FROM usern"
    ."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

返回

返回正确的行数


这里是问题n不能在查询中

$sql = "SELECT id,client_id,is_admin,is_demon"
."FROM usern"
."WHERE is_demo=? && client_id=? && name=? && pass=?n"
."ORDER BY name";

MySQL可以将字符串值“0”转换为在字符串之前用“B”编写这样的代码:

$sql = "SELECT id,client_id,is_admin,is_demon"
    ."FROM usern"
    ."WHERE is_demo=B? && client_id=? && name=? && pass=?n"
    ."ORDER BY name";
$fields = [
    '0', 
    500,
    'test',
    123
];
$sth = $dbh->prepare($sql);
$sth->execute($fields);
链接地址: http://www.djcxy.com/p/66703.html

上一篇: PDO prepared statements with bit fields

下一篇: PHP PDO Prepared Statement MySQL Count of Select where X LIKE