我的If语句与mysql数据不比较?

时间:2014-04-25 09:59:42

标签: php mysql

晕,我在这里得到了一个If声明,但是没有用。 系统只是将我转到我设置的最后一页。 这不是我想要的。 我想要的是当用户登录时,系统自动检查用户属于哪个部门并将其发送到正确的页面。

$departmentsql="SELECT department FROM staff_table";
$departmentsqlresult=mysql_query($departmentsql);
$departmentcount=mysql_num_rows($departmentsqlresult);
if($departmentcount == "A"){
header("location:departmentA.php");
}
else if($departmentcount == "B"){
header("location:departmentB.php");
}
else if ($departmentcount == "C"){
header("location:departmentC.php")
}

系统只是每次都将我发送到departmentC.php,无论我登录哪个用户。 这不是我想要的。 谁能告诉我我的代码有什么问题? 感谢您的回答。感谢

4 个答案:

答案 0 :(得分:1)

您有2个错误:

if($departmentcount == "A"){
  header("location:departmentA.php");
}

>> $departmentcount将包含一个数字值

PHP没有不明确的类型转换,所以当你比较2个值时,它会将它们都视为相同的类型

相比之下,由于您要将$departmentcountchar进行比较,因此它期望$departmentcount也包含char值,但情况并非如此......所以条件对于您的所有ifelseif都变为false,因此没有输出

比较它

if($departmentcount == 10) //compare with number

>> 当您成功比较时,标题不会重定向,因为header的语法也不正确

以这种方式纠正:

header("Location: departmentA.php");
  /* you need a ^^ space here */

答案 1 :(得分:0)

首先,您似乎没有采用登录用户并以这种方式进行查找,目前看来无论登录用户如何,都会返回所有部门。

接下来mysql_num_rows返回该查询中返回的行数,您需要mysql_fetch_row,mysql_fetch_object等

答案 2 :(得分:0)

你错了使用mysql_fetch_array

$departmentsql="SELECT department FROM staff_table";
$departmentsqlresult=mysql_query($departmentsql);
$departmentcount=mysql_fetch_array($departmentsqlresult);
if($departmentcount[0] == "A"){
header("location:departmentA.php");
}
else if($departmentcount[0] == "B"){
header("location:departmentB.php");
}
else if ($departmentcount[0] == "C"){
header("location:departmentC.php")
}

答案 3 :(得分:0)

首先你必须检查basic difference between mysql_num_rows and mysql_fetch_array是什么。正如其他人所建议的,它会正常工作。 如果你仍然面临问题,那么使用“===”。 comparing values in php