简体   繁体   English

CodeIgniter其中值IN(Field1,Field2)

[英]CodeIgniter Where value IN (Field1,Field2)

I need to set query in my auth script, where in WHERE clause I want to use '_MY_POST_VALUE' IN (Field1, Field2) 我需要在我的auth脚本中设置查询,在WHERE子句中我想使用'_MY_POST_VALUE' IN (Field1, Field2)

Final query will be something like this: 最终查询将是这样的:

SELECT * FROM 'myprefix_users' WHERE 'myemail@email.com' IN ('EMAIL','LOGIN') AND PASSWORD=SHA1('mypassword')

I've tried to do this: 我试过这样做:

$this->db->where("'" . mysql_escape_string($_POST['login']) . "' IN (EMAIL,LOGIN)", NULL, FALSE);
$this->db->where("PASSWORD=SHA1('" . mysql_real_escape_string($_POST['password']) . "')");
$userdb = $this->db->get('users');

..but CodeIgniter set prefix to my login/email value and send error: ..但CodeIgniter将前缀设置为我的登录/电子邮件值并发送错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''test@test.com' IN (EMAIL,LOGIN) AND PASSWORD=SHA1('123')' at line 3

SELECT * FROM (`myprefix_users`) WHERE myprefix_'test@test.com' IN (EMAIL,LOGIN) AND PASSWORD=SHA1('123')

I need to use db_prefix in table name, but I don't need it in my WHERE clause, even third param (FALSE) in ->where() don't work for me :( 我需要在表名中使用db_prefix,但我不需要在我的WHERE子句中,甚至第三个参数(FALSE) - > where()对我不起作用:(

How can I solve my problem? 我怎样才能解决我的问题? Any ideas? 有任何想法吗?

Try using Codeigniter's the Input class. 尝试使用Codeigniter的Input类。 It will simplify your code: 它将简化您的代码:

$login = $this->input->post('login', TRUE);
$password = $this->input->post('password', TRUE);
$this->db->where("'$login' IN ", "('EMAIL','LOGIN')", FALSE);
$this->db->where("PASSWORD", "SHA1('$password')", FALSE);
$userdb = $this->db->get('users');

First off, you don't need all the unnecessary functions in your database statements. 首先,您不需要数据库语句中的所有不必要的函数。

Why not do: 为什么不这样做:

$user = $_POST['user'];
$pass = sha1($pass);

$this->db->where("(email = '$user' OR username = '$user') AND password = '$pass'");
$query = $this->db->get('users');

You don't need to worry about mysql escape since the CodeIgniter class will automatically escape them for you. 您无需担心mysql转义,因为CodeIgniter类会自动为您转义它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM