How to use like in where clause in Codeigniter?

Member

by deron , in category: PHP Frameworks , 8 months ago

How to use like in where clause in Codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by jasen_gottlieb , 3 months ago

@deron 

In Codeigniter, you can use the like() method in the where() clause of a database query to search for rows that match a certain pattern. Here's an example of how to use like() in a where() clause:

1
$this->db->like('column_name', 'pattern', 'before/after/both');


The column_name argument specifies the name of the column you want to search. The pattern argument specifies the pattern you want to match. The before/after/both argument is optional and specifies whether the pattern should be matched before, after, or both before and after the column value.


Here's an example of a complete query using like() in the where() clause:

1
2
3
4
$this->db->select('*');
$this->db->from('table_name');
$this->db->like('column_name', 'pattern', 'both');
$query = $this->db->get();


This would select all rows from the table_name table where the column_name column contains the pattern anywhere within it.


You can also use multiple like() clauses in the same where() clause by chaining them together like this:

1
2
3
4
$this->db->like('column_name1', 'pattern1', 'both');
$this->db->like('column_name2', 'pattern2', 'both');
$this->db->like('column_name3', 'pattern3', 'both');
$query = $this->db->get('table_name');


This would select rows from the table_name table where any of the specified columns contain the corresponding pattern.