如果语句重定向

时间:2014-06-25 18:54:11

标签: php mysql

我有以下代码。

$acct="SELECT acctype FROM users WHERE username='$username'";

然后我有这个If语句

if ($acct="customer") {
Header("Location: user/dashboard.php");
}
else {
Header("Location: other/dashboard.php");
}

示例数据库输出

mysql> select * from users;
+----+----------+----------------------------------+----------+----------+----------+
| id | username | password                         | fullname | location | acctype  |
+----+----------+----------------------------------+----------+----------+----------+
| 14 | customer | 91ec1f9324753048c0096d036a694f86 | customer | customer | customer |
| 15 | mfg      | fcc66ac1c0e07a00b56b0dc4c0902567 | mfg      | mfg      | mfg      |
| 16 | designer | 230ace927da4bb74817fa22adc663e0a | designer | designer | designer |
| 19 | both     | f6cb3e816496528d4187db53bc66567f | both     | both     | both     |
+----+----------+----------------------------------+----------+----------+----------+

所以我的目标是让用户在登录时如果他们的acctype是客户转到user / dashboard.php

如果acctype是anthing,则转到other / dashboard.php

由于某种原因,它忽略了acctype并且只是转到第一个if条件。

通过MySQL运行查询将输出正确的响应。

mysql> SELECT acctype FROM users WHERE username='customer';
+----------+
| acctype  |
+----------+
| customer |
+----------+
1 row in set (0.00 sec)

mysql> SELECT acctype FROM users WHERE username='both';
+--------+
| acctype|
+--------+
| both   |
+--------+
1 row in set (0.00 sec)

我哪里错了?

2 个答案:

答案 0 :(得分:0)

这应该有效:

    $query = "SELECT acctype FROM users WHERE username='$username'";
    if($resultSet = $mysqli->query($query)){
      while($col = $resultSet->fetch_field()){
          if($col->acctype == 'customer'){
          Header("Location: user/dashboard.php");
        }else{
           Header("Location: other/dashboard.php");
        }
       }
    }

答案 1 :(得分:0)

所以我同意Rocket你的PHP编码需要一些工作。

首先从请求开始。 你使用mysqli_query调用,然后设置"结果"等于它。

$results = mysqli_query("SELECT acctype FROM users WHERE username='$username'");

然后创建一个变量来读取每一行。

$row = mysql_fetch_row($result1);

我们只想要第一个结果......

if ($row[0]=="customer") {
Header("Location: user/dashboard.php");
}
else {
Header("Location: other/dashboard.php");
}

" $行[0]"将读取数据库中的第一行。

这应该有效。