MySQLi Tutorial on MySQLi Like Clause

we have seen the sql select command to fetch data from the mysql table. we can also use a conditional clause called as the where clause to select the required records.

a where clause with the ‘equal to’ 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 the where clause.

if the sql like clause is used along with the % character, then it will work like a meta character (*) as in unix, while listing out all the files or directories at the command prompt. without a % character, the like clause is very same as the equal to sign along with the where clause.

syntax

the following code block has a generic sql syntax of the select command along with the like clause to fetch data from a mysql table.

select field1, field2,...fieldn table_name1, table_name2...
where field1 like condition1 [and [or]] filed2 = 'somevalue'
  • you can specify any condition using the where clause.

  • you can use the like clause along with the where clause.

  • you can use the like clause in place of the equals to sign.

  • when like is used along with % sign then it will work like a meta character search.

  • you can specify more than one condition using and or or operators.

  • a where...like clause can be used along with delete or update sql command also to specify a condition.

using the like clause at the command prompt

this will use the sql select command with the where...like clause to fetch the selected data from the mysql table – tutorials_tbl.

example

the following example will return all the records from the tutorials_tbl table for which the author name ends with jay

root@host# mysql -u root -p password;
enter password:*******
mysql> use tutorials;
database changed
mysql> select * from tutorials_tbl 
   → where tutorial_author like '%jay';
+-------------+----------------+-----------------+-----------------+
| tutorial_id | tutorial_title | tutorial_author | submission_date |
+-------------+----------------+-----------------+-----------------+
|      3      |  java tutorial |     sanjay      |    2007-05-21   |   
+-------------+----------------+-----------------+-----------------+
1 rows in set (0.01 sec)

mysql>

using like clause inside php script

php uses mysqli query() or mysql_query() function to select records in a mysql table using like clause. this function takes two parameters and returns true on success or false on failure.

syntax

$mysqli→query($sql,$resultmode)

sr.no. parameter & description
1

$sql

required - sql query to select records in a mysql table using like clause.

2

$resultmode

optional - either the constant mysqli_use_result or mysqli_store_result depending on the desired behavior. by default, mysqli_store_result is used.

example

try the following example to select a record using like clause in a table −

copy and paste the following example as mysql_example.php −

<html>
   <head>
      <title>using like clause</title>
   </head>
   <body>
      <?php
         $dbhost = 'localhost';
         $dbuser = 'root';
         $dbpass = 'root@123';
         $dbname = 'tutorials';
         $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
         
         if($mysqli→connect_errno ) {
            printf("connect failed: %s<br />", $mysqli→connect_error);
            exit();
         }
         printf('connected successfully.<br />');
   
         $sql = 'select tutorial_id, tutorial_title, tutorial_author, submission_date from tutorials_tbl where tutorial_author like "mah%"';
		 
         $result = $mysqli→query($sql);
           
         if ($result→num_rows > 0) {
            while($row = $result→fetch_assoc()) {
               printf("id: %s, title: %s, author: %s, date: %d <br />", 
                  $row["tutorial_id"], 
                  $row["tutorial_title"], 
                  $row["tutorial_author"],
                  $row["submission_date"]);               
            }
         } else {
            printf('no record found.<br />');
         }
         mysqli_free_result($result);
         $mysqli→close();
      ?>
   </body>
</html>

output

access the mysql_example.php deployed on apache web server and verify the output. here we've entered multiple records in the table before running the select script.

connected successfully.
id: 1, title: mysql tutorial, author: mahesh, date: 2021
id: 2, title: html tutorial, author: mahesh, date: 2021
id: 3, title: php tutorial, author: mahesh, date: 2021