I have a query that when I use the bindParam() for start and per_page in PDO does not work. However if I add these in directly as seen in query 2 it works fine. I have 2 questions:
- Why does the bindParam() not work in query 1 for :start and :per_page?
These values start and per_page are not inputted by the user. They are taken from elsewhere so do I need to use 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; } }
}
Query 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;
}
}
}
Apologizes if this is a simple question. I'm learning PDO and I'm trying to get a better understanding of it.