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

时间:2022-07-26 15:28:48

I have a page with 3 dropdowns. Team1, Team2 & Venue

我有一个包含3个下拉列表的页面。 Team1,Team2和Venue

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

When the user clicks view, I then query the DB returning results for team1 against team2 either playing at home or away or both (depending on users selection)

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

The code Im using to execute the query follows

用于执行查询的代码如下

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());

    }

The Problem

Regardless if the user selects venue away or both home & away the result returned is always team1 as the hometeam as you can see in the image below:

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

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

In the above example I selected Team1 as Stormers and Team2 as Sharks, I selected venue away to display Stormers record against Sharks when they are playing away from home, but as you can see from the image Stormers still gets displayed as the hometeam.

在上面的例子中,我选择了Team1作为Stormers,Team2选择了Sharks,我选择了场地来展示Stormers在鲨鱼队远离家乡的比赛时的战绩,但正如你从图像中看到的,Stormers仍然会被显示为队友。

If anyone can tell me what I am doing wrong or point me in the right direction it would be greatly appreciated.

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

Thank you in advance

先谢谢你

2 个解决方案

#1


2  

You are doing an assignment operation on your if statements. Make use of == instead of =

您正在对if语句执行赋值操作。使用==而不是=

The

if($venue = "hometeam"){

should be

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

#2


1  

You're currently "assigning" instead of "comparing" with your conditional statements. It being in the "plural" form. You have three which should be == instead of =

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

= is an assignment operator, while == is a comparison operator.

=是赋值运算符,而==是比较运算符。

Missing an extra = see arrows ^

缺少额外=看到箭头^

if($venue = "hometeam")
           ^

else if($venue = "awayteam")
                ^

else if($venue = "all")
                ^

Rewrite:

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());

    }

#1


2  

You are doing an assignment operation on your if statements. Make use of == instead of =

您正在对if语句执行赋值操作。使用==而不是=

The

if($venue = "hometeam"){

should be

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

#2


1  

You're currently "assigning" instead of "comparing" with your conditional statements. It being in the "plural" form. You have three which should be == instead of =

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

= is an assignment operator, while == is a comparison operator.

=是赋值运算符,而==是比较运算符。

Missing an extra = see arrows ^

缺少额外=看到箭头^

if($venue = "hometeam")
           ^

else if($venue = "awayteam")
                ^

else if($venue = "all")
                ^

Rewrite:

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());

    }