mysql怎么查需一半数据,如何检查mysql数据库是否是用户的一部分

This might be a simple question but I have a login page where I want users to authenticate against LDAP and that's working fine.

I created a mysql database where I created a table with the user's logon name and would like a check after the successful logon to verify in my DB that the user trying to authenticate is part of the Administrator group (defined in my database table).

If this is valid then redirect them to url1 and if not redirect them to URL2. At this point I just need help with the SQL query portion as im not too familiar with it.

Localhost

DB name=imc.directory.tool

Table name=tbl_staff

------------------------------------------------

| ID | username | group |

------------------------------------------------

| 1 | username1 | Administrator |

------------------------------------------------

| 2 | username2 | Guest |

------------------------------------------------

| 3 | username3 | Guest |

------------------------------------------------

解决方案

You can use the following inside any SQL statement, and the API of your choosing.

SELECT ID FROM tbl_staff

WHERE username='username1'

AND `group`='Administrator'

or (sidenote: column_x, column_y are column example names):

SELECT column_x, column_y FROM tbl_staff

WHERE username='username1'

AND `group`='Administrator'

You can also do SELECT * to select all columns, but that is often a method many SQL developers do not like to use.

However, you did say this was for ASP.net which is something I do not know anything about.

Sidenote: group is a MySQL reserved word which requires special attention.

Either by wrapping the word in ticks, or using another name such as "groups" for instance.

Here is a PDO method using prepared statements:

$db = new PDO("mysql:host=localhost;dbname=db_name", $user, $pass);

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$username = "username1";

$group = "Administrator";

$query = "SELECT COUNT(*) FROM tbl_staff

WHERE username = :username

AND `group` = :username";

$statement = $db->prepare($query);

$statement->bindValue(':username', $username);

$statement->bindValue(':password', $group);

$statement->execute();

$count = $statement->fetchColumn();

if ($count === 1)

{

return TRUE;

}

else

{

return FALSE;

}

References:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值