subhomoy Posted January 17, 2013 Share Posted January 17, 2013 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... Link to comment https://forums.phpfreaks.com/topic/273281-php-search/ Share on other sites More sharing options...
premiso Posted January 17, 2013 Share Posted January 17, 2013 You will want to use the MySQL LIKE comparison function. $sql= "SELECT * FROM categories WHERE categories_name LIKE '%{$search}%'"; The %'s are wild cards. Link to comment https://forums.phpfreaks.com/topic/273281-php-search/#findComment-1406471 Share on other sites More sharing options...
Psycho Posted January 17, 2013 Share Posted January 17, 2013 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 https://forums.phpfreaks.com/topic/273281-php-search/#findComment-1406472 Share on other sites More sharing options...
stijn0713 Posted January 17, 2013 Share Posted January 17, 2013 clarification of your objective would indeed be usefull: Should it be for example that any 3 consecutive chars that match is ok? say: stijn versus mystic Link to comment https://forums.phpfreaks.com/topic/273281-php-search/#findComment-1406479 Share on other sites More sharing options...
QuickOldCar Posted January 18, 2013 Share Posted January 18, 2013 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; Link to comment https://forums.phpfreaks.com/topic/273281-php-search/#findComment-1406556 Share on other sites More sharing options...
subhomoy Posted January 18, 2013 Author Share Posted January 18, 2013 clarification of your objective would indeed be usefull: Should it be for example that any 3 consecutive chars that match is ok? say: stijn versus mystic yes that is what i exactly want... Link to comment https://forums.phpfreaks.com/topic/273281-php-search/#findComment-1406565 Share on other sites More sharing options...
Barand Posted January 18, 2013 Share Posted January 18, 2013 If you also want to return results that are only 3 characters, you need to edit the minimum word length in mysql. That applies to FULLTEXT searches. Not relevant in this case. Link to comment https://forums.phpfreaks.com/topic/273281-php-search/#findComment-1406566 Share on other sites More sharing options...
stijn0713 Posted January 18, 2013 Share Posted January 18, 2013 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%'"; } Link to comment https://forums.phpfreaks.com/topic/273281-php-search/#findComment-1406631 Share on other sites More sharing options...
subhomoy Posted January 19, 2013 Author Share Posted January 19, 2013 Thanks guysss..... Link to comment https://forums.phpfreaks.com/topic/273281-php-search/#findComment-1406879 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.