Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter: How to use WHERE clause and OR clause

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!

like image 731
tarnfeld Avatar asked Jun 04 '10 16:06

tarnfeld


People also ask

How to use where clause in ci?

You'll notice the use of the $this->db->where() function, enabling you to set the WHERE clause. You can optionally pass this information directly into the update function as a string: $this->db->update('mytable', $data, "id = 4");

Can we use Array in where clause?

We can pass an array with the help of where IN clause.

How do I print a query in CI?

Use the below query to display the query string: print_r($this->db->last_query()); To display the query result follow this: print_r($query);


2 Answers

$where = "name='Joe' AND status='boss' OR status='active'";  $this->db->where($where); 
like image 111
Dylan Avatar answered Oct 20 '22 02:10

Dylan


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 
like image 20
Rookwood Avatar answered Oct 20 '22 02:10

Rookwood