Good evening. In general, I had to deal with such a thing. I have 2 tables. In the first (table1) information about all users. Example:
user_id:3, name:Вася user_id:5, name:Иван user_id:6, name:Оля
In the second (table2) - information about the interactions between these users. Example:
user_id:3, his_best_friend_id:5, his_girlfriend_id:6
And you need this: Pull out the names of Vasya's best friend and girl.
As I see it: we take out the IDs of his best friend and girl from the first table. Then, knowing the ID, we do one more query and pull out the name of both. It looks like this:
$result=mysql_query("SELECT best_friend,girlfriend FROM table2 WHERE user_id='3'"); $row = mysql_fetch_assoc($result) //тут у нас оказываются айдишники 5 и 6 $friend=mysql_query("SELECT name FROM table1 WHERE user_id='".$row['best_friend']."'"); $his_friend=mysql_fetch_assoc($friend); //получаем Иван $girl_friend=mysql_query("SELECT name FROM table1 WHERE user_id='".$row['girlfriend']."'"); //получаем Оля $his_girl_friend=mysql_fetch_assoc($friend);
So there would be no problems, but I get a lot of such Vasya out and it all goes through
while($row = mysql_fetch_assoc($result)) {}
I am 99% sure that this is not the most optimal solution for such a case))
Maybe someone knows a better option? I will be glad to any links and instructions.