Reputation: 83
I have a search function on a page that when the user types in a search term when submit is pressed it checks the database and shows appropriate records in the database (it uses a %Like% query) on the page.
However the user may spell something wrong. Is there anyway it can still search the database and show some results. I.e. Colour is stored in the database however the user searches color - is there any way we can change the WHERE clause to make sure that Colour is pulled through?
Code is below:
//// Contains the search term from the form
$searched_club = $_GET['username'];(form is a PHP_SELF)
if($searched_club == ''){
$sql = "SELECT name, region FROM jos_users ORDER BY id DESC LIMIT 6";
}else{
$sql = "SELECT name, region FROM jos_users WHERE name LIKE '%$searched_club%'";
}
$rsd = mysql_query($sql, $dbconn);
<table>
while($rs = mysql_fetch_array($rsd)) {
$name = $rs['name'];
$region = $rs['region'];
?>
<tr>
<td><?php
echo $name;
?></td>
<td><?php
echo $region;
?></td>
</tr>
<?php
}
?>
</table>
Upvotes: 2
Views: 1524
Reputation: 218837
You can use the string function soundex()
to get a string representing a kind of audible pattern for a supplied string. If two strings are different but would sound similar, they should produce identical soundex()
results. Strings like:
In a SELECT
query, you can use the shortcut SOUNDS LIKE
to compare the soundex()
of two strings:
SELECT * FROM SomeTable WHERE SomeField SOUNDS LIKE SomeInputValue
Upvotes: 3