我正在開發(fā)一個(gè)私人消息系統(tǒng),就像在一個(gè)簡單的論壇網(wǎng)站中一樣。但我不確定 SELECT 查詢是否正確。我試圖在聊天框中顯示每條消息,因此例如用戶A向用戶B發(fā)送了一條消息,而用戶B可能向用戶A發(fā)送了另一條消息。但我不斷收到“問題”字符串(在 else 語句中,不會(huì)查詢......)。 $alici = $_GET['user']; $username = $_SESSION['username']; $mesajlar = $con->prepare("SELECT * FROM messages WHERE sender_name=:s AND receiver_name=:r OR sender_name=:s2, AND receiver_name=:r2"); $mesajlar->execute([':s'=>$username, ':r'=>$alici, ':s2'=>$alici, ':r2'=>$username]); if ($mesajlar->rowCount() > 0) { foreach ($mesajlar as $mesajlar_each) { $sender_name = $mesajlar_each['sender_name']; $receiver_name = $mesajlar_each['receiver_name']; $messsage = $mesajlar_each['message_text']; if($sender_name == $username){ ?> <div class="grey-message"> <a href="#"> Me </a> <p> <?php echo $message; ?> </p> </div> <?php } else{ ?> <div class="white-message"> <a href="#"> <?php echo $sender_name; ?> </a> <p><?php echo $message; ?> </p> </div> <?php } } } else{ //problem echo "problem"; } ?> ```
1 回答

動(dòng)漫人物
TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊
我可能會(huì)這樣寫:
$query = "
SELECT c.olumns
, y.ou
, a.ctually
, n.eed
FROM messages m
WHERE (:s,:r) IN ((m.sender_name,m.receiver_name)
,(m.receiver_name,m.sender_name))";
$mesajlar = $pdo->prepare($query);
$mesajlar->execute(['s' => $username,'r' => $alici]);
$result = $stmt->fetch();
- 1 回答
- 0 關(guān)注
- 136 瀏覽
添加回答
舉報(bào)
0/150
提交
取消