简体   繁体   English

Codeigniter where子句不起作用

[英]Codeigniter where clause is not working

I'm new to codeigniter I've a function in model with this code 我是Codeigniter的新手,我在此代码中的模型中有一个函数

    $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子句之后,应该像这样

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. 我已经搜索了所有类似Active Record Class的地方 ,还搜索了stackoverflow CodeIgniter WHERE子句和其他论坛,但没有找到与我的问题相关的解决方案。 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. 选项1:您可以创建条件字符串并将其传递给where函数。 but Make sure to add space before and after Equals to "=" 但请确保在等于“ =”之前和之后添加空间

same thing mentioned on similar thread here error in codeigniter custom where clause Codeigniter自定义where子句中在类似线程上提到的相同错误

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

Option 2: 选项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 第3行。它正在寻找一列名为

`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. 这也将为您避免使用变量,这更安全。

try like this 这样尝试

$where = "username='".$username."' AND password='".$password."' AND status='1'";

to

$where = "username ='"~~~~~

keep a blank between "first column name" with "operator" 在“第一列名称”和“运算符”之间保持空白

in codeigniter v1.0 在codeigniter v1.0中

Its better to use SHA( $password ) or MD5() value instead Checking with the Hash value directly This code might work..!! 最好使用SHA( $password )或MD5()值代替直接检查哈希值。 and for the query you can make this replacement username= admin AND password= 7c4a8d09ca3762af61e59520943dc26494f8941b AND status='1' LIMIT 1 对于查询,您可以使用以下替换用户名= admin AND密码= 7c4a8d09ca3762af61e59520943dc26494f8941b AND status =' 7c4a8d09ca3762af61e59520943dc26494f8941b 1

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

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