0

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.

Community
  • 1
  • 1
user2727841
  • 715
  • 6
  • 21

7 Answers7

6

Try Like this:

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

$this->db->where($where); 
user2936213
  • 1,021
  • 1
  • 8
  • 19
  • 1
    Is that a question or a sentence? `$this->db->where()` accepts an array of `table=>value` pairs. There's no why, that's how it works – Keith Jan 08 '14 at 05:26
  • 2
    @Prodikl it accepts a custom string too, the issue in the OP is with the backticks, using `$this->db->where($where,NULL,FALSE);` should fix it – omma2289 Jan 08 '14 at 05:34
  • Ah, derp. In that case it's the backticks, yup. I posted an answer, too. – Keith Jan 08 '14 at 05:41
  • how to get proxy ip address using codeigniter??? I know with PHP I've to use $_SERVER['HTTP_X_FORWARDED_FOR'] but in codeigniter what I've to do to grab the proxy ip address??? – user2727841 Jan 08 '14 at 07:31
2

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); 
Community
  • 1
  • 1
1

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();
}
King-of-IT
  • 578
  • 4
  • 18
1

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();
shubomb
  • 672
  • 7
  • 20
0

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.

Keith
  • 4,144
  • 7
  • 25
  • 43
  • how to get proxy ip address using codeigniter??? I know with PHP I've to use $_SERVER['HTTP_X_FORWARDED_FOR'] but in codeigniter what I've to do to grab the proxy ip address??? – user2727841 Jan 08 '14 at 07:49
-1

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

-2

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

user1830049
  • 109
  • 1
  • 6
  • What is the relation of `md5` and `SHA` to do with this question? – user2936213 Jan 08 '14 at 05:36
  • 1
    The error mentions an incorrect column name; it's not a problem with hashes – Keith Jan 08 '14 at 05:42
  • how to get proxy ip address using codeigniter??? I know with PHP I've to use $_SERVER['HTTP_X_FORWARDED_FOR'] but in codeigniter what I've to do to grab the proxy ip address??? – user2727841 Jan 08 '14 at 07:48