好友列表数据库的简单设计 [英] Simple Design of friends list database

查看:467
本文介绍了好友列表数据库的简单设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

建立友谊系统数据库有一个小问题. 现在我有一张叫朋友的桌子 假设:

I have a small issue with making a friendship system database. now I have a table called friends let's say:

表上的朋友:

    you      friend      approve     since
_________________________________________________
   wetube    youtube     1           4-12-2012
   facebook  wetube      1           4-12-2012

并且我有此查询代码来获取名为wetube的用户的朋友.

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'");

现在我确切想要的是如何获取wetube好友并将其显示在他的页面上.

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

已修复:

最后,我解决了这个问题. 这是表格:

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 ;

这是获取用户朋友的php代码

and this is the php code to fetch the user friends

<?php
$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']."<br/>";
} elseif($username == $friends_row['friend']) {
echo $friends_row['you']."<br/>";
}
}
?>

自己尝试一下,它可以100%工作

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'];

这篇关于好友列表数据库的简单设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆