Community Based Support.

MySQL LIKE Clause

MySQL LIKE Clause

We have seen SQL SELECT command to fetch data from MySQL table. We can also use a conditional clause called WHERE clause to select required records.

A WHERE clause with equals sign (=) works fine where we want to do an exact match. Like if “tutorial_author = ‘Sanjay'”. But there may be a requirement where we want to filter out all the results where tutorial_author name should contain “jay”. This can be handled using SQL LIKE clause along with WHERE clause.

If SQL LIKE clause is used along with % characters, then it will work like a meta character (*) in UNIX while listing out all the files or directories at command prompt.

Without a % character, LIKE clause is very similar to equals sign along with WHERE clause.

Syntax:

Here is generic SQL syntax of SELECT command along with LIKE clause to fetch data from MySQL table:

Using LIKE clause inside PHP Script:

You can use similar syntax of WHERE…LIKE clause into PHP function mysql_query(). This function is used to execute SQL command and later another PHP function mysql_fetch_array() can be used to fetch all the selected data if WHERE…LIKE clause is used along with SELECT command.

But if WHERE…LIKE clause is being used with DELETE or UPDATE command, then no further PHP function call is required.

Example:

Try out the following example to return all the records from tutorials_tbl table for which author name contains jay: