2014-07-17 74 views
0

所以我有一個通知系統,這裏的表佈局只得到一個通知,結果

+-----------+------------------+------+-----+---------+----------------+ 
| 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從第一個結果結束「主導」。任何幫助都會很棒。

+0

數據庫中只有一行... –

+0

假設我要添加另一個結果@OzanKurt – user302975

+0

數組中的第一個元素不是「顯性」。這是數組中的第一個元素。如果你想要第二個元素,你需要引用_second_元素'$ notifications [1] ['type']'而不是第一個_first_元素('[0]')。這與sql或mysql沒有任何關係。你會得到與靜態數組相同的行爲。 – spencer7593

回答

1

根據你的代碼的通知$陣列將是這樣的:

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

你的數據庫的每一行會得到一個新元素,這個數組。 因此,您需要遍歷該數組並查看您獲得的值。

+0

但這不是發生了什麼事。所有我得到的是'hgjghj',我把它叫做'$ notifications [0] ['type']' – user302975

+0

@ user302975:如果數組中有第二個條目,那麼它會被引用爲'$ notifications ['** '1'**'] ['type']',對數組中相同第一個元素'[0]'的重複引用將返回相同的值。 – spencer7593

+0

那麼發生了什麼是我得到'注意:未定義索引'當我嘗試做'$ notifications ['type']',所以基本上我怎麼會得到所有? @ spencer7593 – user302975