28

I am using the following code to select from a MySQL database with a Code Igniter webapp:

$query = $this->db->get_where('mytable',array('id'=>10));

This works great! But I want to write the following MySQL statement using the CI library?

SELECT * FROM `mytable` WHERE `id`='10' OR `field`='value'

Any ideas? Thanks!

tarnfeld
  • 25,992
  • 41
  • 111
  • 146

6 Answers6

49
$where = "name='Joe' AND status='boss' OR status='active'";

$this->db->where($where);
Dylan
  • 859
  • 1
  • 8
  • 12
  • though i am only 3 years late but from which table will it get it? how to specify the table – Rana Tallal Jan 11 '14 at 21:34
  • 2
    Though I am 3/4 of a month late, you still execute the following after your where clauses are defined... $this->db->get("tbl_name"); – James Feb 03 '14 at 07:20
  • `$where = '(name="Joe" AND status="boss" OR status="active")';` See: http://stackoverflow.com/a/9941814/594235 – Sparky Oct 12 '14 at 18:38
33

You can use or_where() for that - example from the CI docs:

$this->db->where('name !=', $name);

$this->db->or_where('id >', $id); 

// Produces: WHERE name != 'Joe' OR id > 50
Rookwood
  • 683
  • 1
  • 5
  • 11
13

You can use this :

$this->db->select('*');
$this->db->from('mytable');
$this->db->where(name,'Joe');
$bind = array('boss', 'active');
$this->db->where_in('status', $bind);
Wassim Sboui
  • 1,692
  • 7
  • 30
  • 48
6

Active record method or_where is to be used:

$this->db->select("*")
->from("table_name")
->where("first", $first)
->or_where("second", $second);
Vaviloff
  • 16,282
  • 6
  • 48
  • 56
Raham
  • 4,781
  • 3
  • 24
  • 27
3
$where = "name='Joe' AND status='boss' OR status='active'";

$this->db->where($where);

Though I am 3/4 of a month late, you still execute the following after your where clauses are defined... $this->db->get("tbl_name");

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
Pullat Junaid
  • 3,224
  • 3
  • 25
  • 25
1

What worked for me :

  $where = '';
   /* $this->db->like('ust.title',$query_data['search'])
        ->or_like('usr.f_name',$query_data['search'])
        ->or_like('usr.l_name',$query_data['search']);*/
        $where .= "(ust.title like '%".$query_data['search']."%'";
        $where .= " or usr.f_name like '%".$query_data['search']."%'";
        $where .= "or usr.l_name like '%".$query_data['search']."%')";
        $this->db->where($where);



$datas = $this->db->join(TBL_USERS.' AS usr','ust.user_id=usr.id')
            ->where_in('ust.id', $blog_list) 
            ->select('ust.*,usr.f_name as f_name,usr.email as email,usr.avatar as avatar, usr.sex as sex')
            ->get_where(TBL_GURU_BLOG.' AS ust',[
                'ust.deleted_at'     =>  NULL,
                'ust.status'     =>  1,
            ]); 

I have to do this to create a query like this :

SELECT `ust`.*, `usr`.`f_name` as `f_name`, `usr`.`email` as `email`, `usr`.`avatar` as `avatar`, `usr`.`sex` as `sex` FROM `blog` AS `ust` JOIN `users` AS `usr` ON `ust`.`user_id`=`usr`.`id` WHERE (`ust`.`title` LIKE '%mer%' ESCAPE '!' OR  `usr`.`f_name` LIKE '%lok%' ESCAPE '!' OR  `usr`.`l_name` LIKE '%mer%' ESCAPE '!') AND `ust`.`id` IN('36', '37', '38') AND `ust`.`deleted_at` IS NULL AND `ust`.`status` = 1 ;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
LOKENDRA
  • 384
  • 3
  • 13