我会根据关本指南中先前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