X_JuDaH_X
X_JuDaH_X

Reputation: 33

Search MYSQL by PHP with exact match in field

I have a name field. It comes out with First Name and Last Name. So lets say (Jerry Jones) is name. When they search Jerry it still comes up. But I want to make it exact search only. They have to put in (Jerry Jones)...Reason is there may be two Jerry's and they both come up. Or is there a way in my search form that will say which Jerry do you want and give me the results of them. Then once they pick correct one it redirects to new page with results?

Here is form

<form action="search.php" method="GET">
<input type="text" name="query" />
<input type="submit" value="Search" />
</form>

Search

<?php
mysql_connect("localhost", "username", "password") or die("Error connecting to database: ".mysql_error());


mysql_select_db("ambassador") or die(mysql_error());


    $query = $_GET['query'];


$min_length = 3;


if(strlen($query) >= $min_length){ 

    $query = htmlspecialchars($query);


    $query = mysql_real_escape_string($query);


    $raw_results = mysql_query("SELECT * FROM member
        WHERE (`Name` LIKE '%".$query."%')") or die(mysql_error());



    if(mysql_num_rows($raw_results) > 0){ 

        while($results = mysql_fetch_array($raw_results)){



        }


    }
    else{ 
        echo "No results";
    }

}
else{ 
    echo "Minimum length is ".$min_length;
}
?>

Thanks

Upvotes: 0

Views: 2407

Answers (3)

bklups
bklups

Reputation: 300

Try to use MATCH instead of LIKE

Upvotes: 0

Luiz Pedone
Luiz Pedone

Reputation: 1

Your SQL statement is incorrect: if you want to query for exactly Jerry Jones, you should do: SELECT * FROM members WHERE name = "$query".

Upvotes: 0

Julio
Julio

Reputation: 2290

Try:

mysql_query("SELECT * FROM member WHERE Name='$query'")

This should produce an exact match, which is what I believe you want by may have the unintended side effect of being too limiting.

Upvotes: 1

Related Questions