phpsqlmysqlcodeignitersql-like

How to only append a wildcard at the end of a WHERE LIKE condition with CodeIgniter's active record


I am trying to do this sql query on codeigniter

SELECT * FROM person WHERE type = 'staff' AND description LIKE 'university%'

I am not sure if it is correct...

$this->db
    ->get_where('person' , array('type'=>'staff'))
    ->like('description','university%')
    ->result_array();

Solution

  • Using Active Record you can do so

    $query = $this->db->select('*')
                 ->from('person')
                 ->where('type','staff')
                 ->like('description','university','after')
                 ->get();
    
    $result = $query->result_array();
    

    Make sure you pass after as a third parameter in like() function so active record will add the wild card i.e % after university so it will look like LIKE 'university%'