gpt4 book ai didi

php - 在 php 中 explode

转载 作者:行者123 更新时间:2023-11-29 04:24:38 25 4
gpt4 key购买 nike

在我的表 'users' 中有 'friends' ,

像这样:

+----+------+---------+
| id | name | friends |
+----+------+---------+
| 1 | a | 0,1,2 |
| 2 | b | 0,1,3 |
| 3 | c | 0,1 |
+----+------+---------+

如何使用 explode 函数逐一获取以逗号 (,) 分隔的好友 ID(不是 0,1,2);

如何选择id? (示例):

$sql = Select id from users where id = (exploded)

if (mysql_num_rows($sql) > 0 ) {
$TPL->addbutton('Unfriend');
}else{
$TPL->addbutton('Add as Friend')
}

最佳答案

这里的解决办法其实就是稍微改变一下你的数据库结构。我建议您创建一个“多对多” 关系表,其中包含用户引用的所有用户好友。

+---------+-----------+
| user_id | firend_id |
+---------+-----------+
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 1 |
| 2 | 5 |
+---------+-----------+

如果您在一个字段中存储值列表,那么这是您的数据库设计不是很理想的第一个迹象。如果您需要搜索数值,最好在该字段上放置索引以提高效率并使数据库为您工作,而不是相反:)

然后要查明用户是否是某人的 friend ,您将查询此表 -

SELECT * FROM users_friends WHERE 
`user_id` = CURRENT_USER AND `friend_id` = OTHER_USER

要获得某个用户的所有 friend ,您可以这样做 -

SELECT * FROM users_friends WHERE `user_id` = CURRENT_USER

关于php - 在 php 中 explode ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14456132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com