Databases| SQL| MySQL| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
PHP SQL LIKE Operator 
 

This example illustrates how to use the like operator in sql query.

 

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  matching strings based on the specified pattern in a column.  The "%" wildcard can be used to specify the missing letters in the pattern. This wild card can be used at any place in the pattern. This example takes table emp for the query execution.

 

 

 

 

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

The above query returns only the rows which has data in the name column having pattern '%sh%' i.e. The names which have 'sh' string in between and there may be any string before and after the 'sh' string.

Output:

In the query below, we want to find names which start from s.

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

In the query below, we want to find names which end with h.

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

In the query below, we want to find names in which a must be available.

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

                         

» View all related tutorials
Related Tags: sql mysql c query rest table select io return tab show if condition ie record where records ci ws sh

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.