douhuang3740 2015-01-01 13:03
浏览 10

mysqli查询php函数内部检查userlevel

I'm trying to make a function that checks the user level.

function run_admin($userid, $username){

    global $dbcon;

    $stmt = $dbcon->prepare("SELECT userid, username, admin FROM user WHERE userid = ?");

    $stmt->bind_param('isi',$userid, $username, $admin);
    $userid = $_SESSION['userID'];

    $stmt->execute();
    $stmt->bind_result($userid,$username,$admin);

    $stmt->fetch();

   //if($admin >= 1){ echo $userid . " is admin!"; }
   // above prints "1 is admin!";

}

if(run_admin("1", "Mikkel") == "1")){ 
   echo "It's working, you are admin"; 
}

I've included the file with the $dbcon and it is working on another page. The global is a idea from another question, mysqli/mysql query inside function not working but I can't get it working.

I'd like to use the function to check if session logged in userID and username .

The admin row returns with integers.

I'm not really good at mysqli prepared statements, still learning but this I don't know where to read about.

Could anyone help or guide me in the right direction.

  • 写回答

2条回答 默认 最新

  • duancai9010 2015-01-01 13:11
    关注

    The problem may be in this statements :

    $stmt = $dbcon->prepare("SELECT userid, username, admin FROM user WHERE userid = ?");
    
    $stmt->bind_param('isi',$userid, $username, $admin);
    

    In the first statement, in your query there is only one ? for the userid, so in the second statement you need to bind only one value. So the statement will be :

    $stmt->bind_param('i',$userid);
    
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了