douzhi1972 2014-04-25 09:59
浏览 23

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

Halo, I got a If statement here but is not working. The system just pass me to the last page which I set it. And that is not what I want. What I want here is when the user login, the system automatic check the user belong to which department and send them to the correct page.

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

The system just send me to the departmentC.php every time, no matter which user I login. And that is not what I want. Can anyone tell me whats wrong with my code? Appreciate for your answer. Thanks

  • 写回答

4条回答 默认 最新

  • dongshushi5579 2014-04-25 10:03
    关注

    You have 2 errors :

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

    >> $departmentcount will contain a numric value

    PHP doesn't implictyly typecast, so when you compare 2 values, it treats both of them of same type.

    On comparison, since you are comparing $departmentcount with a char, it'll expect $departmentcount to contain char value too, which is not the case...so condition becomes false for all of your if and elseif, hence no output

    Compare it like

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

    >> when you'll compare successfully, header wont redirect, because your syntax for header is also incorrect

    Correct it this way :

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

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探