我正在开发一个私人消息系统,就像在一个简单的论坛网站中一样。但我不确定 SELECT 查询是否正确。我试图在聊天框中显示每条消息,因此例如用户A向用户B发送了一条消息,而用户B可能向用户A发送了另一条消息。但我不断收到“问题”字符串(在 else 语句中,不会查询......)。 $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 回答
动漫人物
TA贡献1815条经验 获得超10个赞
我可能会这样写:
$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 关注
- 104 浏览
添加回答
举报
0/150
提交
取消