3

I have recently begun converting my backend queries in my codeigniter application to active record pattern. As of now I am using codeigniter 2.1 version. I have originally written a query in mysql as

SELECT * FROM table WHERE (field1 = $field1 OR field2 = $field2) AND field3 = $field3 AND field4 = 'text'

Now I would like to write this thing in a way given below:

$this->db->select('*');
$this->db->from('table');
$this->db->where('field1',$field1);
$this->db->or_where('field2',$field1);
$this->db->where('field3',$field2);
$this->db->where('field4','text');

But somehow the "or_where" function is not working properly. Any solution for this, would be my pleasure?

Sanks
  • 141
  • 1
  • 3
  • 12
  • 1
    *(sidenote)* [Code Igniter's ActiveRecord is **not** an ActiveRecord at all.](http://stackoverflow.com/a/2480944/208809) – Gordon Oct 20 '12 at 08:07

1 Answers1

2

you write custom string in Active Record

$where = "(field1 = " . $field1 . " OR field2 = " . $field2 . ") AND 
          field3 = ". $field3 . " AND field4 = 'text'";
$this->db->where($where);

Click here for source: Active Record (and browse for custom string)

John Woo
  • 258,903
  • 69
  • 498
  • 492
  • Thanks bro... But i thought there would be more secure which didnot involve any plain sql coding in queries. – Sanks Oct 20 '12 at 07:30