Jump to content

php search


subhomoy

Recommended Posts

I am trying to search the database but I failed... It only works if the whole set of charecters that is been searched matches with the database information...

 

My code is follows...

<form method="post" action="">
<table>
<tr>
 <td>Enter the categories</td>
 <td><input type="text" name="search" /></td>
</tr>
<tr>
 <td></td>
 <td><input type="submit" name="submitbutton" value="Search"/></td>
</tr>
</table></form>
<?php
if(isset($_REQUEST['submitbutton'])){
$search= mysql_real_escape_string($_REQUEST['search']);
$sql= "SELECT * FROM categories WHERE categories_name='$search'";
$result= mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
if($search==$row['categories_name']){
 echo "successfull";
}else{
	 echo "Not found";
}
}
}
?>

 

I want to show the required item if any three charecters/letters matches with the database item name......

 

Thanks in advance...

Edited by subhomoy
Link to comment
Share on other sites

Hmm . . . I think you need to clarify this statement

I want to show the required item if any three charecters/letters matches with the database item name......

 

So, are you saying if the user enters "ABCD" you want to find any records that contain the letters "ABC", "ABD", "ACD" or "BCD"?

Link to comment
Share on other sites

If you also want to return results that are only 3 characters, you need to edit the minimum word length in mysql.

 

In /etc/my.cnf or /etc/mysql/my.cnf under the [mysqld] section, have this line:

 

ft_min_word_len = 3

 

Then do a quick repair on the table

 

REPAIR TABLE table_name QUICK;

Edited by QuickOldCar
Link to comment
Share on other sites

my first thought, but i don't know whether performance-wise etc. it's a good idea:

 

1) chunk the searchable string is every possible 3-char-long substring, like:

 

$aSearch = 'Engels';
$zipSearch = str_split($aSearch);
$searchable = array();
if(count($zipSearch) > 3){
for ($i=0; $i < count($zipSearch)-2; $i++){
$searchable[] .= $zipSearch[$i].$zipSearch[$i+1].$zipSearch[$i+2];
}
}

 

2) apply the LIKE statement for every chuck.

 

 

$tableName = 'yourtable';
$sql = "SELECT * FROM myDB WHERE $tableName LIKE '%$searchable[0]%'";

foreach($searchable as $k => $searchChunk){
if($k != 0)
$sql .= " OR $tableName LIKE '%$searchChunk%'";

}

Edited by stijn0713
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.