Problem: I am learning more about SQL queries and I'm trying to understand why this function and query returns only the statuses that have a user_id of 26 and 25 but doesn't return anything from 27 or 31.
Question: Why is this not returning statuses for user 27 or 31? (I have confirmed that user 27 and 31 have statuses to return)
Here is the code below:
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) && user_id IS NOT NULL) OR user_id = :auth_id OR (group_id IN (:group_array) && group_id IS NOT NULL && group_id != 0) ORDER BY updated_at DESC LIMIT :offset, :limit");
$stmt->bindParam(':auth_id', $user_id);
$stmt->bindParam(':group_array', $group_array);
$stmt->bindParam(':friend_array', $friend_array);
$stmt->bindParam(':offset', $start, PDO::PARAM_INT);
$stmt->bindParam(':limit', $per_page, PDO::PARAM_INT);
$stmt->execute();
if ($stmt->rowCount() > 0) {
while($row = $stmt->fetchAll()) {
return $row;
}
}
}
When I print_r($group_array) I get 51, and print_r($friend_array) gets 25,27,31. From what I can see that it is only getting statuses from the first user in the friend array 25 and ignoring 27 and 31.
I tested this inside phpmyadmin using:
SELECT * FROM statuses WHERE (user_id IN (25,27,31) && user_id IS NOT NULL) OR user_id = 26 OR (group_id IN ('51,') && group_id IS NOT NULL && group_id != 0) ORDER BY updated_at DESC LIMIT 0, 10
This worked fine. Any help would be greatly appreciated.