我有一个查询,当我使用bindParam()开始和PDO中的per_page不起作用。但是,如果我直接添加这些,如查询2中所示,它工作正常。我有两个问题:
这些值start和per_page不是由用户输入的。它们来自其他地方所以我需要使用bindParam()?
public function t_status($friends, $groups, $user_id, $start, $per_page, $db){
$group_array = implode(',', $groups);
$friend_array = implode(',', $friends);
$stmt = $db->prepare("SELECT * FROM statuses WHERE user_id IN (:friend_array) OR user_id = :auth_id OR group_id IN (:group_array) ORDER BY updated_at LIMIT $start, $per_page");
$stmt->bindParam(':auth_id', $user_id);
$stmt->bindParam(':group_array', $group_array);
$stmt->bindParam(':friend_array', $friend_array);
$stmt->execute();
if ($stmt->rowCount() > 0) {
while($row = $stmt->fetchAll()) {
return $row;
}
}
}
查询2:
public function t_status($friends, $groups, $user_id, $start, $per_page, $db){
$group_array = implode(',', $groups);
$friend_array = implode(',', $friends);
$stmt = $db->prepare("SELECT * FROM statuses WHERE user_id IN (:friend_array) OR user_id = :auth_id OR group_id IN (:group_array) ORDER BY updated_at LIMIT :start, :per_page");
$stmt->bindParam(':auth_id', $user_id);
$stmt->bindParam(':group_array', $group_array);
$stmt->bindParam(':friend_array', $friend_array);
$stmt->bindParam(':start', $start);
$stmt->bindParam(':per_page', $per_page);
$stmt->execute();
if ($stmt->rowCount() > 0) {
while($row = $stmt->fetchAll()) {
return $row;
}
}
}
如果这是一个简单的问题,请道歉。我正在学习PDO,我正试图更好地理解它。
答案 0 :(得分:2)
更改:
$stmt->bindParam(':start', $start);
$stmt->bindParam(':per_page', $per_page);
到
$stmt->bindParam(':offset', $start, PDO::PARAM_INT);
$stmt->bindParam(':limit', $per_page, PDO::PARAM_INT);