php sql查询没有返回正确的结果

时间:2014-02-01 02:32:48

标签: php mysql sql

我有一个包含3个下拉列表的页面。 Team1,Team2&地点

enter image description here

当用户点击视图时,我会查询数据库返回team1对team2的结果,无论是在家中还是在外面或两者都玩(取决于用户的选择)

用于执行查询的代码如下

if($venue = "hometeam"){
        $result= " SELECT *
FROM `results`
WHERE `hometeam` = '$team1' && `awayteam` = '$team2'" or die(mysql_error());
    }

    else if($venue = "awayteam"){
        $result = " SELECT *
FROM `results`
WHERE `awayteam` = '$team1' && `hometeam` = '$team2'"or die(mysql_error());
    }

    else if($venue ="all"){
        $result = " SELECT *
FROM `results`
WHERE (`hometeam` = '$team1' AND `awayteam` = '$team1') OR (`hometeam` = '$team2' AND `awayteam` = '$team2')"or die(mysql_error());

    }

问题

无论用户选择离开场地还是选择家庭和场所。离开返回的结果总是team1作为家庭团队,如下图所示:

enter image description here

在上面的示例中,我选择了Team1作为Stormers,Team2选择了Sharks,我选择了场地来显示Stormers在远离家乡时对阵鲨鱼的记录,但正如您从图像中看到的那样Stormers仍然显示为家庭团队。

如果有人能告诉我我做错了什么或指出我正确的方向,我们将不胜感激。

提前谢谢

2 个答案:

答案 0 :(得分:2)

您正在对 if 语句进行分配操作。使用==代替=

if($venue = "hometeam"){

应该是

if($venue == "hometeam"){
         //^------ Add one more like this. Do this for your `elseif` too 

答案 1 :(得分:1)

您目前正在“分配”而非“比较”您的条件语句。它是“复数”形式。您有三个==而不是=

=assignment operator,而==comparison operator

缺少额外的=,请参阅箭头^

if($venue = "hometeam")
           ^

else if($venue = "awayteam")
                ^

else if($venue = "all")
                ^

<强>重写:

if($venue == "hometeam"){
        $result= " SELECT *
FROM `results`
WHERE `hometeam` = '$team1' && `awayteam` = '$team2'" or die(mysql_error());
    }

    else if($venue == "awayteam"){
        $result = " SELECT *
FROM `results`
WHERE `awayteam` = '$team1' && `hometeam` = '$team2'"or die(mysql_error());
    }

    else if($venue == "all"){
        $result = " SELECT *
FROM `results`
WHERE (`hometeam` = '$team1' AND `awayteam` = '$team1') OR (`hometeam` = '$team2' AND `awayteam` = '$team2')"or die(mysql_error());

    }