如何在mysql中获取前两行值并将其与会话用户变量的值进行比较? [英] How to fetch top two rows values in mysql and compare it with value of session user variable?
问题描述
我想在我的项目中分配 2 名员工 ID 最少的员工作为管理员.如何从mysql中获取员工ID最少的2行并将其与当前会话用户进行比较?
I want to assign 2 employees with least employee id's as administrator in my project. How to fetch 2 rows with least employee id's from mysql and compare it with current session user?
例如:
EMP_ID |EMP_名称
EMP_ID | EMP_Name
1 | Mike
2 | Peter
3 | Drake
4 | Oliver
5 | Andrew
假设我当前的用户是 Oliver (EMP_ID-4),我如何限制管理页面从其选项卡打开并从除 Mike 和 Peter 之外的用户直接访问 URL?如果管理页面是由 Peter (EMP_ID-2) 或 Mike (EMP_ID-1) 打开的,那么它也应该从管理选项卡和直接 url 访问打开.请帮忙!!
suppose my current user is Oliver (EMP_ID-4), how do i restrict admin page to open from its tab and direct url from users except Mike and Peter? If admin page is opened by Peter (EMP_ID-2) or by Mike (EMP_ID-1) then it should open from admin tab and direct url access too. Please help!!
推荐答案
按照以下步骤操作,
1) 一旦用户注册,前两个用户将是例如比如说,superadmin(1) 和 admin(2) 其余的都是普通用户.
1) Once user will register, first two users will be e.g. say, superadmin(1) and admin(2) rest all will be normal users.
2) 现在当用户登录时,您需要将这些用户数据与 emp_id 一起保存到会话中.
2) Now when a user will login, you need to save those user data along with emp_id into session.
3) 现在,授权意味着哪个用户可以访问哪些页面,您将触发查询,如
3) Now, authorization, means which user will have access to which pages, you will fire query like,
select emp_id from employee order by emp_id asc limit 2.
这个查询只会给你那些排名靠前的用户的 ID,不管是 1 或 2 还是任何数字.
4) 检查当前用户的 emp_id(您已存储在会话中)是否存在于您上面编写的查询中的前两个用户的 emp_id 中,
4) Check if current user's emp_id(which you have stored in session) exists in top two user's emp_id from query which you above wrote,
如果存在,
然后访问所有页面,
其他
显示一条警告消息,表明您没有获得授权,或者只是重定向到您的主页(如果有).
show an alert message, that you are not authorized or just redirect to your home page if any.
我敢肯定,如果您按照以下步骤操作,您现在已经完成了所有操作.
I am sure, you are now done with everything, if you follow these steps.
这篇关于如何在mysql中获取前两行值并将其与会话用户变量的值进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!