简体   繁体   中英

Getting only one notification result

So I have a notification system, here's the tables layout

+-----------+------------------+------+-----+---------+----------------+
| 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    |                |
+-----------+------------------+------+-----+---------+----------------+

And this is what one row looks like

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

Now here's how I'm getting the results

    //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']
    );
}

And now finally my issue, say I had another row, and the type was different, what happens is once I loop the query I get the same type returned throughout the whole, so instead of

Alelx hgjghj scott,

and say I had another result that said

Alelx ghfjhgj scott

I wouldn't be able to see it because the Type from the first result ends up being "dominant". Any help would be great.

According to your code your $notifications array will look like this:

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

Every row of your DB will get another element to this array. So, you need to loop through that array and see what values you get.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM