确定WHERE语句的哪些部分失败 [英] Determine Which Part(s) of WHERE Statement Failed

查看:103
本文介绍了确定WHERE语句的哪些部分失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有一个这样的SQL语句来检查用户登录:

Let's say I have a SQL statement like this that checks a user login:

SELECT * FROM users 
WHERE username='test@example.com', password='abc123', expire_date>=NOW();

SQL中是否有一种方法可以专门确定哪些WHERE条件失败,而不必将每个条件分为自己的查询并单独进行测试?

Is there a way in SQL to determine specifically which WHERE conditions fail, without having to separate each condition into its own query and test individually?

在此特定示例中,它将允许开发人员确切告知用户其登录尝试失败的原因.

In this specific example it would allow the developer to tell users exactly the reason why their attempt to login failed.

出于我的目的,我使用的是PHP/MySQL.

For my purposes I'm using PHP/MySQL.

推荐答案

这是我想出的:

SELECT
  IF(mem_username='test@example.com','true','Error: Bad Username') AS mem_username,
  IF(mem_password ='abc123','true','Error: Bad Password') AS mem_password'
FROM MEMBERS
WHERE mem_username='test@example.com' AND mem_password='abc123'

这样,我可以在代码中检测错误消息,然后根据需要显示它们.

This way I can detect in code for error messages and then display them as necessary.

注意::对于所有引用示例代码的安全性的人,感谢您的关注.但是,这不是真正的生产代码,这只是一个简单的示例来说明我的问题.很明显,您不应以明文形式存储密码,也不应向用户提供有关登录失败原因的详细信息.

NOTE: To all of you citing security concerns with the example code, thank you for your concern. However, this is not real production code this was simply a simple example to demonstrate the question I had. It is quite obvious that you shouldn't store passwords in clear text, and that you shouldn't give users specifics on why login fails.

这篇关于确定WHERE语句的哪些部分失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆