我正在构建一个社区网站,其中添加了添加人员为好友的功能。
现在我的 friend 表看起来像这样
+----+---------+-------+
| id | ppl_1 | ppl_2 |
+----+---------+-------+
| 1 | 5 | 7 |
| 2 | 13 | 5 |
+----+---------+-------+
id is simply the autoincrement field.
ppl_1 is one user's id
ppl_2 is another user's id
现在假设我是 id 为 5 的用户。我想查看我的好友列表。我怎样才能解决这个问题,因为我的 id 可以是 ppl_1
之间的任何位置。或ppl_2
。那么我是否必须运行 2 个查询来检查?考虑到网站的性能,我想这会很糟糕。或者我是否必须在一个查询中执行某些操作来检查两种方式?
考虑$me = $_SESSION['id'] ;
有人给我看一下吗?
最佳答案
如果您重命名表中的 ppl_1 和 ppl_2 列以更好地反射(reflect)它们的实际含义,将会对您有所帮助。
您可以将它们命名为“friend”和“befriended_by”之类的名称
然后您将执行连接:
SELECT befriended_by
FROM user
INNER JOIN [name of your table above]
ON user.id = friend
WHERE user.id = [:id, please use parameterised queries to insert your $_SESSION['id'] here or some other sane method to prevent sql injection attacks ]
就是这样。
关于php - 显示好友列表中的好友,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27320567/