Or another option is to do a Levenshtein/SoundEx comparison.
The soundex() function represents how a word is spoken via a string of characters.
The levenshtein() function represents a numerical distance between two strings.
Using a combination of the two functions, you can create a rather powerful "return best result" search engine.
Code:
<?
// Get the name they entered
$golfer[1] = $_POST['golferentry#2'];
// Return a list of all golfers in db
$sql = @mysql_query("SELECT Golfer FROM tbl_golfers");
// This var will hold the distance between the two strings, for our best match throughout the loop
$shortest = -1;
// Begin looping through Golfers in DB
while ($row = mysql_fetch_array($result)) {
// Get the Levenshtein distance between how the two golfers name's are pronounced
$lev_dist = levenshtein(soundex($golfer[1]), soundex($row['Golfer']));
// Do we have an exact match? If so, store it in the vars and get out of the loops
if ($lev_dist == 0) {
$closest = $row['Golfer'];
$shortest = 0;
break;
}
// Is this lev distance shorter than the last? If so, it's a better match, store it in the vars and continue on to the next comparison
if ($lev_dist <= $shortest || $shortest < 0) {
$closest = $row['Golfer'];
$shortest = $lev_dist;
}
}
echo "Closest Match is: " . $closest . "
";
?>
Using this method golfer#2 will never be null, because it will always return the closest match. This can be a good and/or bad thing. If you're only golfer is Tiger Woods in the database, and the user searches for Marilyn Manson - Tiger Woods will be the closest found and returned. This form of search (and any search method) is only as good as the info in the database. The more golfers you have, the better the chances of the user finding who they want.
EDIT: Commented code