Php 使用准备好的语句从带有限制标记的数据库中进行选择
我使用一个准备好的语句来访问数据库中的一些行,但是当我指定一个限制标记时,我遇到了一个错误——我不知道为什么 这在我的模型中:Php 使用准备好的语句从带有限制标记的数据库中进行选择,php,mysql,pdo,Php,Mysql,Pdo,我使用一个准备好的语句来访问数据库中的一些行,但是当我指定一个限制标记时,我遇到了一个错误——我不知道为什么 这在我的模型中: public function returnData() { $this->limit = 10; $r = $this->db->select("SELECT id,userid,dateTime,rating,description FROM table where userid = :userid ORDER BY dateTime ASC
public function returnData() {
$this->limit = 10;
$r = $this->db->select("SELECT id,userid,dateTime,rating,description FROM table where userid = :userid ORDER BY dateTime ASC LIMIT :limit", array(':userid' => $this->userid, ':limit' => $this->limit));
return $this->_returnData($r);
}
如果我将“限制”替换为下面的数字,一切都会起作用,但我不确定为什么我不能在这里使用准备好的语句-我想知道这是否与数字上的引用有关,但我无法计算出来
ORDER BY dateTime ASC LIMIT 10", array(':userid' => $this->userid));
它访问以下函数:
/*
*Select
*@param string $sql - An SQL string
*@param array $array - An array of parameters to bind. Default is empty array
*@param constant $fetchMode - A PDO fetchmode. Default is fetch_assoc
*return mixed
*/
public function select($sql, $array = array(), $fetchMode = PDO::FETCH_ASSOC) {
$sth = $this->prepare($sql);
foreach ($array as $key => $value) {
$sth->bindValue("$key", $value);
}
$sth->execute();
$this->rowCount = $sth->rowCount();
return $sth->fetchAll($fetchMode);
}
对于限制,绑定时必须将值作为整数传递:
foreach ($array as $key => $value) {
if($value == (int)$value)
$sth->bindValue("$key", $value, PDO::PARAM_INT);
else
$sth->bindValue("$key", $value);
}
可能重复的