mysql 好友列表设计_好友列表数据库的简单设计

bd96500e110b49cbb3cd949968f18be7.png

I have a small issue with making a friendship system database.

now I have a table called friends

let's say:

table friends:

you friend approve since

_________________________________________________

wetube youtube 1 4-12-2012

facebook wetube 1 4-12-2012

and i have this query code to fetch the friends of user called wetube.

mysql_query("SELECT f.* FROM friends f inner join users u on u.username =

f.you WHERE f.friend = 'wetube' UNION ALL SELECT f.* FROM friends f inner join users u on

u.username = f.friend WHERE f.you = 'wetube'");

now what I want exactly is how to fetch the friens of wetube and show it to him on his page.

fixed:

Finally I fixed the problem.

so this is the table:

CREATE TABLE IF NOT EXISTS `friends` (

`id` int(20) NOT NULL AUTO_INCREMENT,

`you` varchar(255) NOT NULL,

`friend` varchar(255) NOT NULL,

`type` varchar(255) NOT NULL,

`since` date NOT NULL,

`message` text NOT NULL,

PRIMARY KEY (`id`),

UNIQUE KEY `id` (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

and this is the php code to fetch the user friends

$username = $_SESSION['username'];

$friends_sql = mysql_query("SELECT * FROM friends WHERE (friend = '$username' OR you = '$username') ");

while($friends_row = mysql_fetch_assoc($friends_sql)){

if ($username == $friends_row['you']) {

echo $friends_row['friend']."
";

} elseif($username == $friends_row['friend']) {

echo $friends_row['you']."
";

}

}

?>

try it yourself it works 100%

解决方案$result = mysql_query("SELECT * FROM friends WHERE friend='wetube'");

$row = mysql_fetch_array($result);

echo $row['friend'], ' - ', $row['since'];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值