我试图让我的LIMIT接受变量$ limit,但它不喜欢我的绑定。这是因为其他$ params是一个数组(字符串)而不是整数?是否有将$ 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;
}
}
答案 0 :(得分:-3)
您不能 - LIMIT
和OFFSET
的参数不能绑定到变量,因为它们只允许是SQL语法中的数字,而不是表达式,并且变量占位符的行为喜欢表达。 (例如,您可能会注意到LIMIT 1+1
也不起作用。)
您需要插入鼻子,插入一点,然后在SQL查询中插入一个值以获得限制。由于这本身就是一种狡猾的事情,你可能希望首先通过intval()
(或类似的)传递限制值,以确保。