2014-04-25 34 views
-1

晕,我在这里得到了一个If语句,但不起作用。 系统只是将我传递给我设置的最后一页。 这不是我想要的。 我在这里想要的是当用户登录时,系统自动检查用户属于哪个部门并将他们发送到正确的页面。我的if语句与mysql数据没有比较?

$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,无论我登录哪个用户。 这不是我想要的。 任何人都可以告诉我什么是我的代码错了吗? 感谢您的回答。由于

+2

mysql_num_rows返回结果中的行数,而不是一个字母。请选择[mysql_fetch_assoc](http://us1.php.net/manual/en/function.mysql-fetch-assoc.php)。最后不要使用mysql_ *它已弃用,请使用mysqli或PDO。 – Dexa

+2

'mysql_num_rows'返回一个数字,但您将它与一个字符串进行比较。 –

回答

1

你有2个错误:

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

>>$departmentcount将包含numric值

PHP不implictyly类型转换,所以当你比较两个值,它把他们两个相同类型

在比较,因为你是一个比较char$departmentcount,它会希望$departmentcount包含字符值太大,这是不是这样的......这样的条件对所有的ifelseif,因此没有输出变为假

比较像

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

>>时,你会成功的比较,头部不会重定向,因为你的header语法也是不正确

正确这样说:

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

当然你*可以*比较一个int到一个字符串在php – PeeHaa

+0

@PeeHaa:更新与解释...检查它是否合适! :) – NoobEditor

+0

最后一个挑剔的评论。 “位置”标题需要完整的绝对URI :-) – PeeHaa

0

首先好像你根本没把登录的用户和这样做的方式,似乎目前你将返回各部门不管是谁已经登录的用户的查找英寸

下一页mysql_num_rows返回该查询你需要和mysql_fetch_row,mysql_fetch_object等

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") 
}