只获取一个通知结果


Getting only one notification result

我有一个通知系统,这是表格布局

+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| id        | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| from_user | varchar(255)     | YES  |     | NULL    |                |
| to_user   | varchar(255)     | YES  |     | NULL    |                |
| type      | varchar(255)     | YES  |     | NULL    |                |
| date      | varchar(255)     | YES  |     | NULL    |                |
+-----------+------------------+------+-----+---------+----------------+

这是一行的样子

+----+-----------+---------+--------+------+
| id | from_user | to_user | type   | date |
+----+-----------+---------+--------+------+
| 32 | allex     | scott   | hgjghj | NULL |
+----+-----------+---------+--------+------+

现在我是这样得到结果的

    //Check if any records of notifications exists & loop em' back
$stmt = $con->prepare("SELECT * FROM notifications WHERE to_user = :user");
$stmt->bindValue(':user', $username, PDO::PARAM_STR);
$stmt->execute();
$notifications = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    $notifications[] =  array(
        'id' => $row['id'],
        'from_user' => $row['from_user'],
        'to_user' => $row['to_user'],
        'type' => $row['type'],
        'date' => $row['date']
    );
}

现在最后我的问题是,假设我有另一行,类型不同,一旦我循环查询我得到相同的type返回整个,所以不是

Alelx hgjghj scott,

然后说我有另一个结果

Alelx ghfjhgj scott

我无法看到它,因为第一个结果的Type最终是"占主导地位的"。任何帮助都太好了。

根据你的代码,你的$notifications数组看起来像这样:

Array
 0 => (... type=>'hgjghj' ...)
 1 => (... type=>'ghfjhgj' ...)

数据库的每一行将获得该数组的另一个元素。所以,你需要循环遍历这个数组看看你得到了什么值