PHP:While循环保存数组中的最后一个值

时间:2017-12-01 15:55:22

标签: php mysql arrays

我从db获取数据并将其保存在数组中,如下所示

$messages = mysqli_query($connect, 'SELECT DISTINCT subjectid, message , time, status     FROM `chat` WHERE  userid='. $userid);
            while ($row =mysqli_fetch_array($messages)) { 
                $data['message']= $row['message'];
                $data['time']= $row['time'];
                $data['status']= $row['status']; 
            } 

问题是它只保存数组中的最后一条记录。请帮我解决这个问题。这就是我得到的

<pre>Array
(
    [message] => some message
    [time] => 1512132743
    [status] => unseen
)
</pre>

1 个答案:

答案 0 :(得分:2)

将它放入二维数组

$messages = mysqli_query($connect, 'SELECT DISTINCT subjectid, message , time, status     FROM `chat` WHERE  userid='. $userid);
            while ($row =mysqli_fetch_array($messages)) { 
                $data[] = array(
                   'message'=> $row['message'],
                   'time'=> $row['time'],
                   'status'=> $row['status']
                );
            }