Home Tutorial Php Sql PHP SQL LIKE Operator

 
 

Share on Google+Share on Google+
PHP SQL LIKE Operator
Posted on: December 10, 2010 at 12:00 AM
Advertisement
This example illustrates how to use the like operator in sql query. The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

PHP SQL LIKE Operator

This example illustrates how to use the like operator in sql query. The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

In the following code the "%" sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern.

Table: emp

 

Source Code of likeOperator.php

<?php
  $con = mysql_connect("localhost","root","root");
  if (!$con) {
    die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("test", $con);

  $result = mysql_query("select * from emp where name like '%sh%'");
  echo "<table border='1'>
    <tr>
      <th>id</th>
      <th>Name</th>
    </tr>";
    while ($row = mysql_fetch_array($result)) {
      echo "<tr>";
      echo "<td>" . $row['emp_id'] "</td>";
      echo "<td>" . $row['name'] "</td>";
      echo "</tr>";
    }
  echo "</table>";
  
  mysql_close($con);
?>

Download Source Code

Output:

In this query we want to find the name which start from s.

$result = mysql_query("select * from emp where name like 's%'");

In this query we want to find the name which end with h.

$result = mysql_query("select * from emp where name like '%h'");

In this query we want to find the name in which a must be available.

$result = mysql_query("select * from emp where name like '%a%'");

Advertisement

Related Tags for PHP SQL LIKE Operator:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 10, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com