duandaiqin6080 2016-09-30 12:42
浏览 6
已采纳

Mysql DB麻烦

I have this table that stores messages for the user. It has id(PK), username, message, time, date. When you log in to your account it shows a list of all of your chats. But this DB only shows the messages. Do I have to create another table somehow connected to the first DB or filter in a way that I get the usernames of the people I'm chatting with? What I mean by getting the usernames is that I have this list of the people I have messaged. Is there a way I could get those usernames?

I am sorry if this is not clear. If you need more explanation, tell me right away. This problem is quite complex for me.

By the way, here is the code:

 <div id='messages'>
        <nav>
            <ul id=''>
                <?php 
                    $m = new mysqli('localhost', 'root', 'xamppprogram', 'blue_messenger');
                    if($m === FALSE){
                        include('./errordb2.php');
                        die();
                    }

                    $sql = "SELECT * FROM messages WHERE username='$username'";
                    $results = $m->query($sql);
                    if($results->num_rows > 0){
                        while($r = $results->fetch_assoc()){
                            // What to do here?
                        }
                    } else {
                        echo "No new messages.";
                    }
                ?>
            <ul>
        <nav>
    </div>
    <div id='chat-content'>
  • 写回答

1条回答 默认 最新

  • doujin4031 2016-09-30 12:52
    关注

    You could add another column on the table, like toUser that stores the name of the user you're chatting with. Then you can use a GROUP BY toUser condition in your sql. This would be the easiest solution, but as others have stated in the comments of the question, a relational database would be best.

    You would be best to use a prepared statement since user input seems to be used here and would be open to an SQL injection.

    References:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。