检查用户是否为管理员

时间:2017-05-20 17:58:33

标签: php mysql

我有问题。在我的数据库中,我添加了一个名为admin的列,如果你是一个管理员,则值为1,如果不是,则值为0.但是如何在php中进行查询,选择用户以检查该用户是否为管理员?

这就是我现在所拥有的。

<?php
$db = new PDO('mysql:host=localhost;dbname=id1552202_accounts', 'id1552202_thecouch', 'Fargo123');   
 $admincheck = $db->prepare(" SELECT Admin From Accounts WHERE Usernames = :username ");
              $admincheck->bindParam("username", $_SESSION['loggeduser'][0]");
  $admincheck->execute();
$admin = $admincheck->fetch();
if($admin === 0){
  echo "you're an admin";
} else {
  echo "you're not an admin";
}
?>

2 个答案:

答案 0 :(得分:1)

You mentioned if you are an admin then 1 else 0 but while checking 

if($admin === 0){
  echo "you're an admin";
} else {
 echo "you're not an admin";
}

I think you are checking the opposite way for the admin

答案 1 :(得分:1)

您正在使用ssh bar@vargrant.box并提交错误返回,还需要从数组if($admin === 0){获取$admin['Admin']而不是bindParam :username

username

如果要检查用户是否具有代码

以下的管理员凭据,请替换上面的查询
 $admincheck = $db->prepare("SELECT Admin From Accounts WHERE Usernames = :username ");
 $admincheck->bindParam(":username", $_SESSION['loggeduser'], PDO::PARAM_STR);
 $admincheck->execute();
 $admin = $admincheck->fetch(PDO::FETCH_ASSOC);

    if($admin['Admin'] == 1){
      echo "you're an admin";
    } 
    else {
      echo "you're not an admin";
    }

此方法显示所有管理员用户

$admincheck = $db->prepare("SELECT Usernames, Admin From Accounts WHERE Usernames = :username");