I'm going based off this guide in a previous StackOverflow post:
"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 bob@bob.com bobbie M 1/1/2009 New York City
2 jon@jon.com jonathan M 2/2/2008 Los Angeles
3 joe@joe.com joseph M 1/2/2007 Pittsburgh
Table Friends
---------------
UserID FriendID
----------------
1 2
1 3
2 3"
Now, I have all that done, and I've created this query:
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);
Yay! This print_r gets the associated array that I expected. But now, because either user_id OR friend_id can be the logged in user, I'm stumped s to what kind of query I need to go with to be able to actually display the friends list.
$query = "SELECT * ";
$query .= "FROM users ";
$query .= "WHERE id = ''
This is about as far as I've gotten due to my confusion. Any point in the right direction would be amazing. thank you!
PHP
$reg_no = $_SESSION["user_id"];
SQL QUERY:
select * from `User` join `friends` on '$reg_no' = `friends`.`UserID` and `User`.`UserID` = `friends`.`UserID` or '$reg_no' = `FriendID` and `User`.`UserID` = `friends`.`UserID`;
TRY IT ....
Oh I get it. You (the user) want to display your friends (other users)
Try this:
SELECT U.EmailAddress, U.Gender, U.DOB, U.Location FROM User U
LEFT JOIN Friends F on U.UserID = F.Friend_ID WHERE F.User_ID = 1;
And then just change 1 to whatever the logged in user's id is.
Cheers!
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.