提问人: 提问时间:3/24/2020 最后编辑:Funk Forty Niner 更新时间:3/24/2020 访问量:50
查询数据库中的行,如果返回结果,检查 column = $_SESSION 值,然后做些什么?[复制]
Query database for rows, if returns result, check if column = $_SESSION value, then do something? [duplicate]
问:
我是PHP和MySQL的初学者。我正在尝试制作一个聊天应用程序,用户可以在其中打开他们的聊天线程并显示消息。我正在尝试查询聊天并在适当的块中回显消息。是否可以检查列的 true 是否为 true,然后在其中输出消息?然后为每一行回声?对不起,我真的是新手,所以我不知道所有正确的过程和 SQL 命令。谢谢!messages
message
div
If statement
$_SESSION['uid']
session_start();
$stmt = $conn->prepare("
SELECT message from messages WHERE conv_id=?
AND (user_send=? OR user_receive=?)
ORDER BY timestamp ASC
");
$stmt->bind_param('iii', $_POST['conv_id'], $_SESSION['uid'],$_SESSION['uid']);
$stmt->execute();
$stmt->bind_result($message);
$message_right = '';
$message_left = '';
while($stmt->fetch()){
if (user_send = $_SESSION['uid']){
$message_right .= '<div class="display-on-right">'.$message.'</div>';
echo $message_right;
}elseif(user_receive = $_SESSION['uid']){
$message_left .= '<div class="display-on-left">.$message.</div>';
echo $message_left;
}
}
答:
-2赞
2 revsAkintunde-Rotimi
#1
你所做的很简单。
将所需的列添加到查询中,然后就可以在从查询中获取结果后访问它。
$stmt = $conn->prepare("
SELECT message, user_send, user_receive from messages WHERE conv_id=?
AND (user_send=? OR user_receive=?)
ORDER BY timestamp ASC");
$stmt->bind_result($message, $user_send, $user_receive);
if ($user_send == $_SESSION['uid']){ //single equals to is for assigning. double is for comparing
//do something
}
评论
0赞
3/24/2020
是的,但我需要根据它们添加到数据库的顺序正确排序它们。以及显示在右侧的当前发送用户和左侧显示的当前接收用户
评论
mysqli_error($conn)
user_send