-2

所以,我想知道如何在 if 语句中将一个查询的结果与另一个查询的结果进行比较。像这样:

$team = mysql_query("SELECT teamId FROM team WHERE teamName='$teamName'");
$tplayer = mysql_query("SELECT teamId FROM player WHERE playerName='$playerName'");

if($team==$lplayer){
//Do something
}
else{
//Do something else
}

这不起作用......为什么?

现在,为什么这不起作用:

$tleague = mysql_query("SELECT teamId from team
    WHERE leagueId=(SELECT leagueId FROM league WHERE leagueName='$leagueName')");
$tplayer = mysql_query("SELECT teamId FROM player WHERE playerName='$playerName'");

$row1 = mysql_fetch_array($tleague);
$row2 = mysql_fetch_array($tplayer);

if($row1['teamId']==$row2['teamId']){}
else{}
4

5 回答 5

1

您需要mysql_fetch_assoc()在查询上使用,并比较返回的值。像下面这样的东西。您要比较的是两个返回的资源对象:

$team = mysql_query("SELECT teamId FROM team WHERE teamName='$teamName'");
$tplayer = mysql_query("SELECT teamId FROM player WHERE playerName='$playerName'");
$t = mysql_fetch_assoc($team);
$p = mysql_fetch_assoc($tplayer);
if($t['teamId'] ==$p['teamId']){
//Do something
}
else{
//Do something else
}

但是,您不应该使用mysql_*方法,而是使用MySQLi // Tutorial

于 2013-04-14T18:38:16.803 回答
1

您没有从查询中获取任何结果。做类似的事情

$team_result = mysql_fetch_array($team);
$tplayer_result = mysql_fetch_array($tplayer);

然后使用获取的结果来制作你的if条件

if($team_result['teamId'] == $tplayer_result['teamId'])
{
    //do something
}

也请停止使用mysql,因为它已被弃用,切换到PDOmysqli用于新项目

Update

新查询有错误。为什么不使用联接

$tleague = mysql_query("SELECT a.`teamId` from `team` a LEFT JOIN `league` b ON a.`leagueId` = b.`leagueId` WHERE b.`leagueName` = '$leagueName'");
$tplayer = mysql_query("SELECT `teamId` FROM `player` WHERE `playerName`='$playerName'");

$row1 = mysql_fetch_array($tleague);
$row2 = mysql_fetch_array($tplayer);

if($row1['teamId']==$row2['teamId'])
{
// do something
}
else
{  
// do something else
}

UPDATED AGAIN

我将所有查询合并为一个,并将数据封装在查询'".$playerName."''".$leagueName."'

$query = mysql_query("SELECT a.`teamId` from `team` a LEFT JOIN `league` b ON a.`leagueId` = b.`leagueId` LEFT JOIN `player` c ON b.`teamId` = c.`teamId` WHERE b.`leagueName` = '".$leagueName."' and c.`playerName`= '".$playerName."'");
if($row = mysql_fetch_array($query))
{
    echo 'Found: ' . $row['teamId'];
}
else
{ 
    echo 'Not Found.';
}  
于 2013-04-14T18:39:15.427 回答
0

那样做

   $team = mysql_query("SELECT teamId FROM team WHERE teamName='$teamName'");
   $tplayer = mysql_query("SELECT teamId FROM player WHERE playerName='$playerName'");
   $row1 = mysql_fetch_array($team);
   $row2 = mysql_fetch_array($tplayer);

if($row1['teamId']==$row2['teamId']){
 //Do something
}
 else{
 //Do something else
}

编辑:

在您的第二次编辑中,您的问题在于查询本身。

试试这个

    $tleague = mysql_query("SELECT t.teamId from team inner join league l On t.leagueId = l.leagueId  
 WHERE t.leagueName='".$leagueName."' ");
于 2013-04-14T18:37:39.263 回答
0

它不起作用,因为它不是查询的结果。您需要将 mysql_result 传递给变量,即:

$result1 = mysql_result($team, 0);
$result2 = mysql_result($tplayer, 0);
if ($result == $result2) { ...
于 2013-04-14T18:38:24.613 回答
0

像这样试试

$teamQuery = mysql_query("SELECT teamId FROM team WHERE teamName='$teamName'");
$lplayerQuery = mysql_query("SELECT teamId FROM player WHERE playerName='$playerName'");

$team = mysql_fetch_assoc($teamQuery);
$lplayer = mysql_fetch_assoc($lplayerQuery);

if($team['teamId']==$lplayer['teamId']){
//Do something
}
else{
//Do something else
}
于 2013-04-14T18:38:54.263 回答