简体   繁体   中英

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)

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:

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 :(

How can I solve my problem? Any ideas?

Try using Codeigniter's the Input class. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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