我更新了这个问题,因为它被指出要更改查询以及如何使用implode。在阅读建议的SO帖子后,我再次添加了查询。我还在学习PDO,所以如果我犯了错误,我会道歉。
以下是查询:
$group_array = implode(',', $groups);
$friend_array = implode(',', $friends);
$stmt = $db->prepare("SELECT *
FROM statuses
WHERE parent_id = 0
AND ( user_id = :auth_id
OR FIND_IN_SET(group_id, :group_array)
OR FIND_IN_SET(user_id, :friend_array)
)
ORDER BY updated_at DESC 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;
}
}
当我var_dump朋友和小组时,我得到以下结果:
friend array:array(2){[0] => string(2)“25”[1] => string(2)“27”}
组数组:array(2){[0] => NULL [1] => NULL}
当我使用以下查询对其进行测试时,它仅返回用户ID为26(auth_id)的状态,而不返回朋友数组id为25或27的状态。
SELECT * FROM statuses WHERE parent_id = 0 AND (user_id = 26 OR FIND_IN_SET(group_id, '') OR FIND_IN_SET(user_id, 27)) ORDER BY updated_at DESC LIMIT 0, 10
谁能告诉我对此有所了解?