title: How to write custom queries in codeigniter that includes OR, AND, LIKE
body: <p>I am having two tables from which i have to fetch the records based on Search criteria..
  here is the actual query which i tried earlier without using active record.</p>
  
  <p><strong>Without active record:</strong></p>
  
  <pre><code>$query = $this-&gt;db-&gt;query("select A.* from A LEFT JOIN B ON A.AId=B.BId where
          (A.Title like '%$searchstring%' OR B.Name like '%$searchstring%') and A.Status='1' and A.Type IN (0) and
          (A.UserId = '$userid' or A.Public='0')  Limit $start,20");
  </code></pre>
  
  <blockquote>
    <p>Please note those parenthesis which i have enclosed like this<br>
    <code>(A.Title like '%$searchstring%' OR B.Name like '%$searchstring%')</code></p>
  </blockquote>
  
  <p>Actually this seems to be working fine if the searchstring is <strong>plaintext i.e without quotations</strong>. But if it contains <strong>'</strong> or <strong>"</strong> quotes it returns database error</p>
  
  <p>So i tried writing active record this way</p>
  
  <pre><code>        $this-&gt;db-&gt;select("A.*");
          $this-&gt;db-&gt;from('Bloop as A');
          $this-&gt;db-&gt;join('Keywords as B', 'A.BloopId=B.BloopId','LEFT');
          $this-&gt;db-&gt;where_in('A.Type','1');
          $this-&gt;db-&gt;where('A.Status','1');
          $this-&gt;db-&gt;like('A.Title', $string);
          $this-&gt;db-&gt;OR_like('B.Name', $string);
          $this-&gt;db-&gt;where('A.UserId',$userid);
          $this-&gt;db-&gt;or_where('A.Public',0);
          $this-&gt;db-&gt;limit(20,$start);
          $query = $this-&gt;db-&gt;get();
  </code></pre>
  
  <p>But this returned incorrect results, The reason is the <strong>OR</strong> condition is <strong>not enclosed in parenthesis</strong>. </p>
  
  <p>Can someone correct my query..  </p>
  
  <p>Thanks..</p>