RiaanV RiaanV - 7 months ago 12
PHP Question

Search worked now it's not working

Hi Guys for some reason my search function that worked does not work anymore I have tried variety of like / or_like to no avail could someone please lend a hand here is my controllers code:

function contractor(){
$keyword = $this->input->post('keyword');

$this->db->like($keyword);
$this->db->like('contractor_location',$keyword);
$this->db->like('contractor_email',$keyword);
$this->db->like('contractor_description',$keyword);
$this->db->like('contractor_number',$keyword);
$this->db->like('contractor_website',$keyword);
$this->db->join('category','contractors.contractorID=category_name.catagory_id','inner');
$this->db->group_by('contractor.contractorID');
$query = $this->db->get('contractors');
return $query->result();
}


UPDATE:
Here is the error message I am receiving sorry I forgot to add it:

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'LIKE '%%' ESCAPE '!' AND `contractor_location` LIKE '%%' ESCAPE
'!' AND `contr' at line 4
SELECT * FROM `contractors` INNER JOIN `category` ON
`contractors`.`contractorID`=`category_name`.`catagory_id` WHERE LIKE
'%%' ESCAPE '!' AND `contractor_location` LIKE '%%' ESCAPE '!' AND
`contractor_email` LIKE '%%' ESCAPE '!' AND `contractor_description`
LIKE '%%' ESCAPE '!' AND `contractor_number` LIKE '%%' ESCAPE '!' AND
`contractor_website` LIKE '%%' ESCAPE '!' GROUP BY
`contractor`.`contractorID`

Filename: modules/search/models/Search_m.php

Line Number: 38

Answer

Your code miss column name

    $this->db->like($keyword);

And you have error in table join

 $this->db->join('category','contractors.contractorID=category_name.catagory_id','inner');

Corrected code:

function contractor(){
        $keyword = $this->input->post('keyword');
        $this->db->like('contractor_location',$keyword);
        $this->db->like('contractor_email',$keyword);
        $this->db->like('contractor_description',$keyword);
        $this->db->like('contractor_number',$keyword);
        $this->db->like('contractor_website',$keyword);
        $this->db->join('category','contractors.contractorID=category.catagory_id','inner');
        $this->db->group_by('contractor.contractorID');
        $query = $this->db->get('contractors');
        return $query->result();
}