gpt4 book ai didi

php - 从 mysql/php 中的用户 ID 检索消息

转载 作者:行者123 更新时间:2023-11-30 01:18:55 24 4
gpt4 key购买 nike

由于我的上一个问题非常模糊,我想我应该澄清一些事情。我正在学习如何构建私有(private)聊天应用程序(仅出于教育原因),并且我正在尝试找出进行私有(private)聊天的最佳方式。我创建了 2 个表,userchat。在 chat 中,我有 5 列:

  • message_id
  • 用户 ID
  • 用户名
  • 消息
  • 发送

我只想检索来自所选 user_id 的消息。

现在,这是我的发送表单。输入名称 send = 我要检索的数据。在我掌握窍门之前,我只想检索 user_id 号码(因此您可以在文本区域中输入一个号码来与该用户名聊天)。

<form name="form" method="post">
<input type="text" name="send" id="send" value="">
<input name="user_id" type="hidden" value="<?php echo $_SESSION['user_id'];?>" id="user_id">
<textarea name="message" class="message" onkeydown="if (event.keyCode == 13) document.getElementById('submit').click()"></textarea>
<input type="submit" name="Submit" id="submit" value="">
</form>

这是我的 php 检索表单

<?php
class Chat extends Core {
public function fetchMessages() {
$this->query("
SELECT chat.message,
users.username,
users.user_id,
chat.send
FROM chat
JOIN users
ON chat.user_id //and chat.send = users.user_id
ORDER BY chat.timestamp
DESC
");

return $this->rows();
}

}

基本上,我想将条件设置为:

<?php
$send=$_POST['send'];
$userid=$_POST['user_id'];
if ($send == $userid || (isset($_POST['user_id']))) //this isn't correct- just trying to display my though
{
//retrieve message from user_id number in sql table, allowing the people that have the same user_id as send to only read the message.
}
?>

基本上,我怎样才能只检索特定 user_id 号码的信息,以及我应该在哪里放置这个条件?

最佳答案

我不太确定你的意思,但如果你想从特定的用户ID获取消息,你可以这样做:

if(isset($_POST['getMsgsById']) && isset($_POST['userid'])){
$userid = (int)$_POST['userid'];

$chat = new Chat(); //


if(!$msgs = $chat->getMsgsByUserId($userid)){
echo "No messages found";
}else{
$length = count($msgs);

for($i=0;$i<$length;$i++){
echo $msgs[$i]['msg']. '<br />';
}

}
}


class Chat{

public function getMsgsByUserId($id){
$result = mysql_query("SELECT * FROM your_message_table WHERE userid=$id");
if(mysql_num_rows($result) == 0)
return array();

while($info = mysql_fetch_assoc($result)){
$arr[] = $info;
}

return $arr;
}

}

当然,这只是非常快的事情。

关于php - 从 mysql/php 中的用户 ID 检索消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18775958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com