我會根據關本指南中先前StackOverflow的帖子:PHP/MySQL的 - 好友列表SQL查詢
"Table Name: User
Columns:
UserID PK
EmailAddress
Password
Gender
DOB
Location
TableName: Friends
Columns:
UserID PK FK
FriendID PK FK
(This table features a composite primary key made up of the two foreign
keys, both pointing back to the user table. One ID will point to the
logged in user, the other ID will point to the individual friend
of that user)
Example Usage:
Table User
--------------
UserID EmailAddress Password Gender DOB Location
------------------------------------------------------
1 [email protected] bobbie M 1/1/2009 New York City
2 [email protected] jonathan M 2/2/2008 Los Angeles
3 [email protected] joseph M 1/2/2007 Pittsburgh
Table Friends
---------------
UserID FriendID
----------------
1 2
1 3
2 3"
現在,我已經全部完成了,我已經創造了這個查詢:
if ($_SESSION["user_id"]) {
$user_id = $_SESSION["user_id"];
$query = "SELECT * ";
$query .= "FROM friends ";
$query .= "WHERE ";
$query .= "user_id OR friend_id = '{$user_id}' ";
$result = mysqli_query($connection, $query);
$result_set = mysqli_fetch_assoc($result);
print_r($result_set);
耶!這個print_r獲得我期望的關聯數組。 但是現在,因爲user_id或friend_id可以是登錄用戶,所以我很難理解我需要怎樣的查詢來實際顯示好友列表。
$query = "SELECT * ";
$query .= "FROM users ";
$query .= "WHERE id = ''
這是因爲我得到了,因爲我的混亂。 正確方向的任何一點都會令人驚歎。謝謝!
除了沒有'$ user_id'值,你寫的第二個查詢出了什麼問題?什麼返回? – 2014-09-12 19:51:39