这个PHP IF声明有什么问题?

时间:2013-11-15 11:25:17

标签: php mysql if-statement

我有一个PHP IF语句,可以根据IF条件将不同的SQL结果保存在PHP变量($ sql)中,但它会根据一个条件(第一个条件)不断返回SQL结果,无论如何用户在POST' d值中输入的内容。

当单独输入phpMyAdmin时,所有SQL语句都按预期工作(同时将$ row3和$ row4更改为存在的实际值),而不是PHP IF语句。

任何人都可以看到我在这里做错了什么,如果可能的话,建议我需要采取哪些不同的做法?我知道我没有PHP / MySQL专家,但我很难过:(

非常感谢任何帮助或建议。提前致谢。

$row3 = $_POST['groups'];
$row4 = $_POST['othercode-all'];

IF ($row3='-all-' && ($row4='-all-')) 
{
$sql ="SELECT
  email,
  accountgroup, othercode
FROM
  (SELECT
        email AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email2 AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email3 AS email,
        accountgroup, othercode
   FROM
        accounts) account
WHERE
  email LIKE '%@%'";
}


ELSEif ($row3!='-all-' && ($row4='-all-')) 
{
$sql ="SELECT
  email,
  accountgroup, othercode
FROM
  (SELECT
        email AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email2 AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email3 AS email,
        accountgroup, othercode
   FROM
        accounts) account
WHERE
  email LIKE '%@%'
  AND accountgroup = '$row3'";
}


ELSEIF ($row4 != '-all-' && ($row3 = '-all-'))
{
$sql ="SELECT
  email,
  accountgroup, othercode
FROM
  (SELECT
        email AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email2 AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email3 AS email,
        accountgroup, othercode
   FROM
        accounts) account
WHERE
  email LIKE '%@%'
  AND othercode = '$row4'";
}

ELSE
{
$sql ="SELECT   
  email,
  accountgroup, othercode
FROM
  (SELECT
        email AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email2 AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email3 AS email,
        accountgroup, othercode
   FROM
        accounts) account
WHERE
  email LIKE '%@%'
  AND accountgroup = '$row3' AND othercode = '$row4'";
}

3 个答案:

答案 0 :(得分:9)

试试吧

if ($row3 == '-all-' && $row4 == '-all-') {
     // Do the stuff
} elseif ($row3 != '-all-' && $row4 == '-all-') {
     // Do another
}   

您没有检查是否正在分配。

1)=会将右侧值分配给左侧。在您的情况下,它总是true因为您正在分配字符串。

2)但是==只会检查左侧和右侧的值/变量。

3)===还将检查它们的数据类型,int,float或string ..etc

我可以看到,在每种情况下,所有查询都是相同的,但在where条件下变化很小。所以你最好采取常见的查询,并在条件中附加where条件。它可读也可靠。

答案 1 :(得分:1)

当您使用单个等号时,您编写的第一个语句将始终返回true。 && amp;之后也不需要内部支架。操作

IF ($row3='-all-' && ($row4='-all-')) {}

应该是:

if ($row3 == '-all-' && $row4 == '-all-') {}

如果我要写

if ($var = 'test') {}

当$ var的值成功设置并返回时,它将始终评估为true。我能想到的唯一情况是,如果你将一个变量作为值传递,它就不会存在或者被评估为false。

在您的情况下,您需要比较运算符'==',因为您没有测试是否可以设置值,而是它是一个特定值。

由于每次在其他地方声明它也是有意义的,所以你也在测试相同的值:

$str = '-all-';
if ($row3 == $str && $row4 == $str) { //logic }

===运算符也检查类型,在这种情况下不会有用。 如果要比较var以查看它是否验证为TRUE布尔值,那么它非常有用,因为非false / null / empty值的计算结果为TRUE,即使它不是TRUE布尔值:

$t1 = 'FALSE';
$t2 = FALSE;

if ($t1 === FALSE)
//evaluates as false as t2 is a string not a boolean
if ($t2 === FALSE)
//evaluates as true

最后,由于您没有将条件分组在一起,因此也不需要括号。

希望有所帮助。

答案 2 :(得分:0)

您也可以尝试:

$row3 = ($_POST['groups']=='-all-') ? '%' : $_POST['groups'];
$row4 = ($_POST['othercode-all']=='-all-') ? '%' : $_POST['othercode-all'];

$sql ="SELECT   
  email,
  accountgroup, othercode
FROM
  (SELECT
        email AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email2 AS email,
        accountgroup, othercode
   FROM
        accounts
   UNION ALL
   SELECT
        email3 AS email,
        accountgroup, othercode
   FROM
        accounts) account
WHERE
  email LIKE '%@%'
  AND accountgroup = '$row3' AND othercode = '$row4'";

这样代码就更简单了。你总是使用where子句,但是当它的值是-all - 时搜索任何东西。

相关问题