1

I created a search function in my website that is able to search across all columns. In my accounts table, I have accounts wherein they have their first name and last name. If I search for John, it shows all results, and if I search for Smith, it also shows them. However, I cannot search for John Smith. I tried to concatenate two columns but I get errors.

Controller:

function search_homeowner($searchquery)
    {
      $this->db->select('*')->from('accounts');
      $this->db->where('(role= 0 AND isActive= 1)',NULL,FALSE);
      $this->db->where('(CONCAT(firstname, ' ', lastname) LIKE "%'.$searchquery .'%" OR firstname LIKE "%'.$searchquery .'%" OR lastname LIKE "%'.$searchquery .'%" OR username LIKE "%'.$searchquery .'%" OR address LIKE "%'.$searchquery .'%" )',NULL,FALSE);
      $query = $this->db->get();
      print_r($this->db->last_query());

        if($query->num_rows() > 0)
        {
            return $query->result();
        }
        else
        {
            return $query->result();
        }

    }`

The code above gives me an error:

Parse error: syntax error, unexpected '', lastname) LIKE "%'' (T_CONSTANT_ENCAPSED_STRING) in C:\xampp\htdocs\pgevCI\application\models\model_accounts.php on line 119

LF00
  • 27,015
  • 29
  • 156
  • 295
coderszx
  • 317
  • 3
  • 22
  • It was the apostrophe, which was needed to be a quotation mark. `$this->db->where('(CONCAT(firstname," ",lastname) LIKE "%'.$searchquery .'%" OR firstname LIKE "%'.$searchquery .'%" OR lastname LIKE "%'.$searchquery .'%" OR username LIKE "%'.$searchquery .'%" OR address LIKE "%'.$searchquery .'%" )',NULL,FALSE);` – coderszx Dec 31 '16 at 13:28

0 Answers0