何らかの理由で、このクエリを実行すると、同じデータが 2 回 (double) 返されます。データはデータベースに一度だけ存在します。ダブルスを返すのはなぜですか?私はそれを理解することはできません。
注: この種のクエリは初めてなので、ガイダンスをいただければ幸いです。
SELECT
`conversations_messages`.`message_date`,
`conversations_messages`.`message_date` > `conversations_members`.`conversation_last_view` AS `message_unread`,
`conversations_messages`.`message_text`,
`users`.`username`
FROM `conversations_messages`
INNER JOIN `users` ON `conversations_messages`.`user_id` = `users`.`user_id`
INNER JOIN `conversations_members` ON `conversations_messages`.`conversation_id` = `conversations_members`.`conversation_id`
WHERE `conversations_messages`.`conversation_id` = {$conversation_id}
ORDER BY `conversations_messages`.`message_date` DESC
..... これが関数全体です。
function fetch_conversation_messages ($conversation_id) {
$conversation_id=(int)$conversation_id;
$sql="SELECT
`conversations_messages`.`message_date`,
`conversations_messages`.`message_date` > `conversations_members`.`conversation_last_view` AS `message_unread`,
`conversations_messages`.`message_text`,
`users`.`username`
FROM `conversations_messages`
INNER JOIN `users` ON `conversations_messages`.`user_id` = `users`.`user_id`
INNER JOIN `conversations_members` ON `conversations_messages`.`conversation_id` = `conversations_members`.`conversation_id`
WHERE `conversations_messages`.`conversation_id` = {$conversation_id}
ORDER BY `conversations_messages`.`message_date` DESC";
$result = mysqli_query($sql);
$messages = array();
while (($row= mysqli_fetch_assoc($result)) !== false){
$messages[] = array(
'date' => $row['message_date'],
'unread' => $row['message_unread'],
'text' => $row['message_text'],
'username' => $row['username'],
);
}
return $messages;
}