Solved

codeigniter query  returning too many results

Posted on 2016-10-29
3
50 Views
Last Modified: 2016-10-29
I've created a codeigniter query for a search function using the query builder class and it returns too many results. How could I adjust it to get a more specific result set?

My codeigniter query (shortened version):

function get_print_search() {
$match = $this->input->post('search');
$this->db->like('ExhibitionE','Provenance Online');  
$this->db->like('DepartmentE','Jewish Art');
$this->db->like('TitleE',$match);
$this->db->or_like('ArtistE',$match);
$this->db->or_like('ObjectNameE',$match);                  
$query = $this->db->get('combh');
return $query->result();
}

The type of sql query I would like to use:
SELECT TitleE, ArtistE, ObjectNameE FROM combh WHERE (ExhibitionE Like 'Provenance Online' AND DepartmentE Like 'Jewish Art' ) AND (TitleE Like $match OR ArtistE Like $match OR ObjectNameE Like $match)

There seems to be some confusion in the codeigniter query about where the parenthesis separate the query parts in the sql query. The current codeigniter query  returns all the TitleE, ArtistE and ObjectNameE results from  'Provenance Online' and I would like to get just the  'Provenance Online'  results from 'Jewish Art'.
Thanks for any assistance.
0
Comment
Question by:avir
3 Comments
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41865087
Can you give me few rows and the expected output?
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 41865102
Yeah - CodeIgniter doesn't get on with adding parenthesis, so you'll need to build them manually:

$this->db
    ->select("TitleE, ArtistE, ObjectNameE")
    ->from("combh")
    ->like("ExhibitionE","Provenance Online")
    ->like("DepartmentE","Jewish Art")
    ->where("(TitleE LIKE '%$match%' OR ArtistE LIKE '%$match%' OR ObjectNameE LIKE '%$match%')");

return $this->db->get()->result();

Open in new window

1
 

Author Closing Comment

by:avir
ID: 41865112
Worked great. Thanks.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What to do when Windows Update is not working correctly? What tools can I use to detect the cause of the malfunction problem? What does this numeric error code mean? These and other questions that you have been asking in the past are answered here (…
OfficeMate Freezes on login or does not load after login credentials are input.
In this Micro Tutorial viewers will learn how they can get their files copied out from their unbootable system without need to use recovery services. As an example non-bootable Windows 2012R2 installation is used which has boot problems.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question