douyun6781 2018-03-16 12:01
浏览 49
已采纳

MySQL查询不返回PHP中的值

So i am running the same query on phpmyadmin and in my php code.

The query is :

SELECT comments.roomID,comments.message, comments.dateTimeSent, sender.fname ,sender.lname,receiver.fname, receiver.lname
FROM comments
INNER JOIN users as sender ON comments.senderID = sender.id
INNER JOIN users as receiver ON comments.receiverID = receiver.id
INNER JOIN chatRooms ON comments.roomID = chatRooms.id WHERE comments.roomID = 8;

when i run this directly from phpmyadmin panel the result i get is this

enter image description here

But when i run this in my php code i get this as the result:

Array
(
    [roomID] => 8
    [message] => Hello from mysql database
    [dateTimeSent] => 2018-03-16 11:04:03
    [id] => 23
    [fname] => pavlos
    [lname] => elpidorou
)
Array
(
    [roomID] => 8
    [message] => asdasd;asda
    [dateTimeSent] => 2018-03-16 11:21:30
    [id] => 25
    [fname] => Antreas
    [lname] => antoniou
)

the sender.fname, sender.lname, receiver.fname,receiver.lname are missing from the array

the code i use to execute the query and get the results is as follows

foreach ($chatRoomArray as &$room) {
            $roomID = $room['id'];

            $query = "SELECT comments.roomID,comments.message, comments.dateTimeSent,sender.id, sender.fname ,sender.lname,receiver.fname, receiver.lname
                      FROM comments
                      INNER JOIN users as sender ON comments.senderID = sender.id
                      INNER JOIN users as receiver ON comments.receiverID = receiver.id
                      INNER JOIN chatRooms ON comments.roomID = chatRooms.id WHERE comments.roomID = ".$roomID;


            $stmt = $this->conn->prepare($query);
            $result = $stmt->execute();

            $commentArray = array();

            if ($result) {
                $num = $stmt->rowCount();
                if ($num > 0) {
                    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                        extract($row);
                        print_r($row);
                        $comment = array(
                            "commentID" => $row['id'],
                            "message"=>$row['message'],
                            "dateTimeSent"=>$row['dateTimeSent'],
                            "senderFname"=>$row['sender.fname'],
                            "senderLname" => $row['sender.lname'],
                            "receiverFname" => $row['receiver.fname'],
                            "receiverLname" => $row['receiver.lname']
                        );

                    }
                }

            }
        }
  • 写回答

2条回答 默认 最新

  • dqx24298 2018-03-16 12:05
    关注

    PHP + MySQL won’t include the table name in a field name.

    When you’re running your query, give them a name in the SELECT part.

    SELECT `sender`.`fname` AS `sender_fname`, ...
    

    Then it’ll be in your array as sender_fname. I would avoid using dots in your names.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图