我试图让我的LIMIT接受变量$ limit,但它不喜欢我的绑定。这是因为其他$ params是一个数组(字符串),而不是一个整数?有没有办法将$ limit指定为整数?使用PDO将变量绑定到LIMIT
public function getPhotos($limit, $status, $type, $session, $name, $inc_suite = NULL) {
$params = array();
$sql = 'SELECT * FROM images WHERE 1=1';
/**
* Filter by status
*/
if (!is_null($status) and $status != 'all') {
$sql .=' AND status = :status';
$params[':status']= $status;
}
/**
* Filter by type
*/
if ($type !="null") {
$sql .=' AND type = :type';
$params[':type'] = $type;
}
/**
* Filter by session
*/
if ($session !="null") {
$sql .=' AND session = :session';
$params[':session'] = $session;
}
/**
* Filter by handle
*/
if ($name !="") {
$sql .=' AND handle = :name';
$params[':name'] = $name;
}
/**
* Block Suite photos
*/
if($inc_suite != 1) {
$sql .= " AND type != 'suite'";
}
/**
* Set order and limit the number of photos
*/
$sql .=' ORDER BY created DESC LIMIT :limit';
$params[':limit'] = (int)$limit;
try {
/**
* Prepare the query
*/
$stmt = $this->pdo->prepare($sql);
/**
* Fire the lasers
*/
$stmt->execute($params);
/**
* Grab results
*/
$result = $stmt->fetchAll();
return $result;
} catch(PDOException $e) {
$errors['database'] = $e->getMessage();
return FALSE;
}
}
这已在[另一个问题](http://stackoverflow.com/questions/2269840/php-pdo-bindvalue-in-limit)中解决,其中解决方案是指导PDO更好一点。你可能需要绑定一个特定的变量,而不是直接使用'execute'。希望有人可以解释这是如何适用于你的情况作为更正式的答案。 – tadman
几次回顾这个问题后,我相信这个错误出现在我的语法中,因为我正在使用简写来绑定这些变量。 $ params [':limit'] = $ limit; – jessicaldale
由于某些原因,此变量不会绑定到占位符。有什么想法吗? – jessicaldale