duanjiagu0655 2019-04-06 17:15
浏览 76

mysqli_num_rows没有显示所需的结果

I have a 'users' table in my Database (currently 4 users) that contains different roles (1 = user, 2= admin, 3= moderator). I want to show some lines of my php script only to admins and moderators, so I made this query: (I know it is not secure)

$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$stuff = 'SELECT * FROM `users` WHERE role > 0';
$result = mysqli_query($conn, $stuff);

This query list all users with the role over 0.

require_once('link_to_script.php');
if (mysqli_num_rows($result) >1){
echo "Admin area";
}else{
echo "No Permissions";
}

This should be self explaining (only show "Admin area" to users with role higher than 1)

I don't know what my mistake is and I hope someone could help me a little bit :)

  • 写回答

3条回答 默认 最新

  • dongyue0225 2019-04-06 17:18
    关注

    You can use IN in your SQL Query, For example if you need admin and moderator use the following

     SELECT * FROM `users` WHERE role IN ('2','3')
    

    mysqli_num_rows returning number of rows in result set.

    if (mysqli_num_rows($result)){
       echo "Admin and moderator area";
    }else{
      echo "No Permissions";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算