0

我正在我的网站上编写一个聊天室,但我对 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 是如何被索引的,我也不知道如何以更简单的方式进行查询

有人可以帮忙吗?

4

1 回答 1

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;
}
?>
于 2012-07-15T18:54:25.960 回答