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条)

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答