I have friends table where there are stored two friends’ IDs. When I query, let’s say my friends, is there an easy way to get my friends’ names from users table? I use PHP and MySQL and the tables are not connected by foreign key but I think I can connect them (using Navicat).
My query:
session_start();
if(isset($_SESSION['valid_user']))
{
$currentuser= $_SESSION['valid_user'];
}
$friendships= mysql_query("SELECT * FROM friends WHERE Person1 = '$currentuser' OR Person2 = '$currentuser' ");
while($row = mysql_fetch_assoc($friendsips))
{
if ($row['Person1'] == $currentuser) echo $row['Person2']; // quering the users name from here is hard and long thing to do
if ($row['Person2'] == $currentuser) echo $row['Person1'];
}
friends(friendshipID, Person1, Person2)
users(ID, name, surname)
Assuming the table structure is :
Users:
userid , name
Friends:
friendship_id , userid_1 , userid_2
Edit:
If you want to get both names you can use this.
You can adjust the where clause to query on either user ids.