我在我的网站上编写了一个聊天室,但我对php很新。我希望用户能够与在游戏的相同团队中玩的用户聊天(知道用户可以一起参与不同的团队)以及在同一区域工作的用户。
假设有三个表:帐户用户表,区域可用,游戏'表
我有一个函数可以返回看起来像
的查询function myfunction($userid){
$games_user=mysql_query('select theme from games where games.userid="'.$userid.'"');
$games_theme = mysql_fetch_array($games_user);
$sql = ("select userid, username, area.userid
from account
left join area
on account.userid = area.userid
left join games
on account.userid = games.userid
where account.userid <> '".mysql_real_escape_string($userid)."' and '".(in_array(games.theme,$games_theme))."' and area.userid=1
);
return $sql;
}
重新格式化:
$sql = "
SELECT userid, username, area.userid
FROM account
LEFT JOIN area ON account.userid = area.userid
LEFT JOIN games ON account.userid = games.userid
WHERE account.userid <> '".mysql_real_escape_string($userid)."'
AND '".(in_array(games.theme,$games_theme))."'
AND area.userid = 1
";
但它确实不起作用,我认为我有语法问题。 我真的不明白in_array是如何编入索引的,而且我不知道如何以更简单的方式进行查询
有人可以帮忙吗?
答案 0 :(得分:2)
我仍然不完全确定你在做什么,但我认为这就是你想要的;您可以在一个查询中执行此操作:
<?php
function myfunction($userid){
$id = mysql_real_escape_string($userid);
$sql = "SELECT userid, username, area.userid
FROM account
LEFT JOIN area
ON account.userid = area.userid
LEFT JOIN games
ON account.userid = games.userid
WHERE account.userid<>'$id' AND area.userid=1
AND games.theme IN (SELECT theme FROM games WHERE games.userid='$id')
";
return $sql;
}
?>