|
| |
SQL for keyword Search |
Search options are common in web pages and we can give option to visitors to search for keywords to locate the article or data within a site. Here we will apply the search to an existing table. The visitors will enter one or more than one keywords in the search box and we have to develop a query based on the selection of the visitor. Here we will discuss how to create a sql query to apply to the database with multiple keywords. Basically we will focus on construction of sql using keywords and will not go into details of getting the result or displaying in proper format with page breaks.
Here we will give the option to the visitor to search for exact match or any where match on the table. For this we will use our student table and apply the search to the name field of the MySQL table. Based on the selected type of search we will generate the sql. Here are the steps required to develop a keyword search.
Once the form is submitted check if the visitor has asked for exact match or any where match using if condition. Before that we will remove the blank space from left and right of the search sting by using ltrim and rtrim functions.
$search_text=ltrim($search_text);
$search_text=rtrim($search_text);
If the visitor has asked for exact match then create the query using simple where condition. Else ..
If the visitor has asked for any where matching of keywords then read the search term and break it into array of keywords using split command. Then loop through all the element of the array of words and create the sql command using like command for each word or the element of the array. Here is the code for this.
$kt=split(" ",$search_text);//Breaking the string to array of words
// Now let us generate the sql
while(list($key,$val)=each($kt)){
if($val<>" " and strlen($val) > 0){$q .= " name like '%$val%' or ";}
}// end of while
You can see we have broken the search text using split command and then looped through the keywords. Here using one if condition we have taken care that blank space are removed in formatting the sql string.
This way we will be adding one sql like command with OR combination for each word used.
We will be adding each like command to the string with an SQL OR command. This way we will end with an extra OR command. This extra OR command we can remove from the end by using substr and strlen string functions.
$q=substr($q,0,(strLen($q)-3));
In the above line we have first calculated the length of the string by using strlen and then used that value inside the substr function after subtracting 3 from it. The 3 is subtracted as length of OR with one blank space is 3. This way we will get the string after removing 3 chars from the end. ( that is extra OR with a blank space )
Once this sql is formatted then we can print it to the screen to check the syntax of the sql to match our requirement. Beyond this point you can use any server side script to collect the results from the table. You can read the tutorial on how to display data from mysql using php. You can down load the PHP version of this tutorial in a zip file at the end of this page. Here is the code till now.
$todo=$_POST['todo'];
if(isset($todo) and $todo=="search"){
$search_text=$_POST['search_text'];
$type=$_POST['type'];
$search_text=ltrim($search_text);
$search_text=rtrim($search_text);
if($type<>"any"){
$query="select * from student where name='$search_text'";
}else{
$kt=split(" ",$search_text);//Breaking the string to array of words
// Now let us generate the sql
while(list($key,$val)=each($kt)){
if($val<>" " and strlen($val) > 0){$q .= " name like '%$val%' or ";}
}// end of while
$q=substr($q,0,(strLen($q)-3));
// this will remove the last or from the string.
$query="select * from student where $q ";
} // end of if else based on type value
echo $query;
echo "<br><br>";
$nt=mysql_query($query);
echo mysql_error();
while($row=mysql_fetch_array($nt)){
echo "$row[name],$row[class]<br>";
}
// End if form submitted
}else{
echo "<form method=post action=''><input type=hidden name=todo value=search>
<input type=text name=search_text ><input type=submit value=Search><br>
<input type=radio name=type value=any checked>Match any where <input type=radio name=type value=exact>Exact Match
</form>
";
}
Read how dynamically generate SQL query based on user inputs for searching a database table
Download the zip file for this tutorial
| |
|
| HOME |
| SQL Tutorial List |
| SQL (Home) |
| SQL Commands |
|
|
|
|
|
| Subscribe |
|
Submit your email address and receive
article and product notifications. Your email is safe with us.
|
|
|
|