当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Codeigniter where clause is not working

问题描述:

I'm new to codeigniter I've a function in model 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. Thanks in advance.

网友答案:

Try Like this:

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

$this->db->where($where); 
网友答案:

Option 1: You can make a condition string and pass it to where function. but Make sure to add space before and after Equals to "="

same thing mentioned on similar thread here error in codeigniter custom where clause

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

Option 2:

Write code like below.

$where = array('username' => $username, 
               'password' => $password, 
               'status'   => '1');
$this->db->where($where); 
网友答案:

try below code...

  function get_user(){
    $this->db->select('id, username, password');
            $where = "username=$username AND password=$password AND status='1'";
    $this->db->where($where);
            $this->db->limit(1);
    $query = $this->db->get('YOUR TABLE NAME');
    return $query->row_array();
}
网友答案:

You may also try this code.

    $this->db->select("id, username, password");
    $this->db->from(TABLE_USERS);
    $this->db->where('username', $username);
    $this->db->where('password', $password);
    $this->db->where('status', 1);
    $this->db->limit(1);
    $login = $this->db->get();
网友答案:

Your column names are wrong, look at this error:

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

Line 3. It's looking for a column named

`username='admin'`

Where it should be

`username`='admin' // the tick marks (`) should wrap the column name

Try this:

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

But honestly the best way is to use an array, or else you risk confusion:

Like this:

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

That will escape your variables for you, too, which is safer.

网友答案:

Its better to use SHA($password) or MD5() value instead Checking with the Hash value directly This code might work..!! and for the query you can make this replacement username=admin AND password=7c4a8d09ca3762af61e59520943dc26494f8941b AND status='1' LIMIT 1

分享给朋友:
您可能感兴趣的文章:
随机阅读: