pdo预处理limit出现错误
我的sql如下
select * from table limit ?,?
然后传值
$stmt = $this->_connection->prepare($_sql); $stmt->execute($_val);结果出现这个错误
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''0','12'' at line 1请问怎么解决?
网友回复
这主要是由于limit 后的两个参数,mysql默认只能传数字类型的值,但是php pdo默认没有设置类型的话会以string的方式传值,这就导致出现了这个错误,可以设置一下
在数据库$pdo这块添加
<?php
$pdo = new PDO('mysql:host=localhost;dbname=restful;charset=utf8','root','root');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
return $pdo;
?>
这样就不会出现int转化为string了,这是预处理问题导致的