I'm doing a search function with PHP and MySQL for a football site that you can search for players from all over. The person would type in the last name of the player and it would then pull the information out of the database to display it. pretty simple.
But if someone types the name wrong, nothing will show up. Is there anyway to do a search with PHP and MySQL that would search and pull up somethiing similiar to what they typed?
An example:
Say a players last name is Patterson, but the person searches for Paterson with only one "t", is there a way to let the script now to pull out the closet match?
This is what I have:
Here is the search page:
<form name="search" action="search.php" method="post">
<input type="text" name="name" size="24">
<input type="submit" name="Search Players">
</form>
And here is search.php:
<?
// open connection to database
$connection = mysql_connect("localhost", "user", "pass") or die ("Unable
to connect!");
mysql_select_db("databaseName") or die ("Unable to select database!");
$search = $_POST['name']; // the name the user entered and submitted.
$name_array = array(); // to hold all the unique names returned from your queries.
$len = strlen($search);
if ($len >= 1) {
$on = 1;
do {
$q = "SELECT concat(name_l, ', ', name_f) as pname FROM " .
"databaseName WHERE name_l like '" .
substr($search, 0, $on) . "%' OR name_f like '" .
substr($search, 0, $on) . "%'";
$res = mysql_query($q);
for ($i = 0; $i < mysql_num_rows($res); $i++) {
$row = mysql_fetch_array($res);
$pname = $row['pname'];
if (!in_array($pname, $name_array)) {
$name_array[] = $pname;
}
}
$on++;
} while ($on <= $len);
}
?>
I want people to be able to do a search on the players last name. That field in the database is name_l
I would like it to pull out a name(s) name even if they spell the name wrong.
Is there anyone that can help me get this to work?