Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

codeigniter query  returning too many results

Posted on 2016-10-29
3
Medium Priority
?
102 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 35

Expert Comment

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

Accepted Solution

by:
Chris Stanyon earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This tutorial will walk an individual through the process of configuring basic necessities in order to use the 2010 version of Data Protection Manager. These include storage, agents, and protection jobs. Launch Data Protection Manager from the deskt…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

971 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