 <p>For the benefit of those of you who land up here in search of a working implementation. The following code works for every search criteria no matter how complex. I am using Codeigniter, just for the record. </p> <pre><code> if($this-&gt;input-&gt;get('_search')=="true"){ $where = ""; $ops = array( 'eq'=&gt;'=', 'ne'=&gt;'&lt;&gt;', 'lt'=&gt;'&lt;', 'le'=&gt;'&lt;=', 'gt'=&gt;'&gt;', 'ge'=&gt;'&gt;=', 'bw'=&gt;'LIKE', 'bn'=&gt;'NOT LIKE', 'in'=&gt;'LIKE', 'ni'=&gt;'NOT LIKE', 'ew'=&gt;'LIKE', 'en'=&gt;'NOT LIKE', 'cn'=&gt;'LIKE', 'nc'=&gt;'NOT LIKE' ); $filters = json_decode($this-&gt;input-&gt;get('filters'), true); $first = true; foreach ( $filters['rules'] as $item){ $where .= $first ? " WHERE" : " AND "; $searchString = $item['data']; if($item['op'] == 'eq' ) $searchString = "'".$searchString."'"; if($item['op'] == 'bw' || $item['op'] == 'bn') $searchString = "'".$searchString."%'"; if($item['op'] == 'ew' || $item['op'] == 'en' ) $searchString = "'%".$searchString."'"; if($item['op'] == 'cn' || $item['op'] == 'nc' || $item['op'] == 'in' || $item['op'] == 'ni') $searchString = "'%".$searchString."%'"; $where .= " ".$item['field']." ".$ops[$item['op']]." ".$searchString; if ($first) { $first = false; } } $sql = "SELECT * FROM applicant".$where; $query = $this-&gt;db-&gt;query($sql); $result = $query-&gt;result_array(); $i=0; foreach ($result as $myrow){ $responce-&gt;rows[$i]['id']=$myrow['id']; $responce-&gt;rows[$i]['cell']=array($myrow['id'],$myrow['contingent_id'],$myrow['firstname'],$myrow['lastname'],$myrow['age'],$myrow['grouptype_id'],$myrow['registrationtype_id']); $i++; } } </code></pre>