0

I have an sql search query that returns the matched values within a where clause. I need to update this - so for example, if someone searches "elephhant" it will return the result "elephant". How do I go about doing this? Thanks

$the_word = GET['word_eng'];
$split = substr($the_word, 0, 3);

if ($noOfWords == 1) {
 $searchString = " word_eng  LIKE '" . $the_word ."%'";

}



else {
   $searchString = $whereClause = "";
   $orderBy = " order by case `word_eng` ";
   foreach ($word as $order=>$entry) {
    $searchString .= " OR word_eng LIKE '" . $entry . "'";
    $orderBy .= " when '$entry' then $order ";
   }
   $orderBy .= " end ";
}

$whereClause = ($searchString != "") ? " WHERE " . preg_replace('/OR/',
'', $searchString, 1) : $whereClause;


$sql = "SELECT word_eng FROM words " . $whereClause . " OR  word_eng  LIKE '" .$split "%' " .$orderBy." LIMIT 50";




$result = $conn->query($sql);
johnyTran
  • 5
  • 4
  • Possible duplicate of [MYSQL search for right words | fixing spelling errors](https://stackoverflow.com/questions/35212058/mysql-search-for-right-words-fixing-spelling-errors) – Michel Jul 15 '18 at 09:47
  • can you show us what exactly do you want in where condition.? – Shivrudra Jul 15 '18 at 09:47
  • Depends on how tolerant the match should be. As an entry point for a research you might find the [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) interesting (the article also links other, less sophisticated metrics). If you tag your DBMS you might also get more specific answers. Something, that is implemented in some DBMS is [Soundex](https://en.wikipedia.org/wiki/Soundex), it might be useful for you too. – sticky bit Jul 15 '18 at 09:49
  • Thanks Sticky bit I'll look into Soundex further. For now I think I just want to return a result for every query. Shivrudra - I'd like the where clause to go "return the result $searchStrings " word_eng LIKE $the_word% and if not return the result matching word_eng LIKE $split% but my OR Condition doesn't work? I'll update my code to demonstrate – johnyTran Jul 15 '18 at 10:06

0 Answers0