phpmysqlcodeigniteractiverecordwhere-clause

CodeIgniter active record query is not rendered correctly when multiple conditions are passed to where() method as a single string


I've a model method with this code:

$this->db->select("id, username, password");
$this->db->from(TABLE_USERS);

$where = "username='".$username."' AND password='".$password."' AND status='1'";
$this->db->where($where);

$this->db->limit(1);
$login = $this->db->get();

echo '<pre>';
    print_r($login);
echo '</pre>';
die('Call');

And it is giving this error:

A Database Error Occurred

Error Number: 1054

Unknown column 'username='admin'' in 'where clause'

SELECT `id`, `username`, `password` FROM (`users`) WHERE `username='admin'` AND password='7c4a8d09ca3762af61e59520943dc26494f8941b' AND status='1' LIMIT 1

Filename: C:\wamp\www\customadmin\system\database\DB_driver.php

Line Number: 330

after where clause this should be like this

WHERE `username`='admin' AND `password`='7c4a8d09ca3762af61e59520943dc26494f8941b'

instead of this

WHERE `username='admin'` AND password='7c4a8d09ca3762af61e59520943dc26494f8941b'

I've searched every where like Active Record Class and also at stackoverflow CodeIgniter WHERE clause and other forums but didn't find solution related with my problem.


Solution

  • Try Like this:

    $where = array('username' => $username, 'password' => $password, 'status' => '1');
    
    $this->db->where($where);