PDA

View Full Version : searching problem please help


xiaodao
02-15-2005, 03:27 AM
basically i want to search a database to get similar results as keywords

//search.html
<table width=170 cellpadding=5 cellspacing=1 bgcolor=#ffffff>
<form name="search" method="post" action="search.php">
<tr bgcolor=#f5f5f5>
<td>search options</td>
<td>
<select name="ways">
<option value="title">according to title</option>
<option value="author">according to author</option>
</td>
</tr>
<tr>
<td>
keyword</td>
<td>
<input type="text" name="keywords" maxlength="15" size="7">
</td>
</tr>
<tr>
<td colspan=2>
<input type=submit name=submit value="submit">
</td>
</tr>
</form>
</table>


//my search.php i tried to use LIKE method but i cannot get the result
$ways=$_POST['ways'];
$keywords=$_POST['keywords'];
echo $ways;
echo $keywords;
if(isset($_POST[submit])) {
$sql=$db->query("SELECT * FROM pa_book WHERE ".$ways." LIKE %".$keywords."%");
if(!sql) {
echo "cannot find the book";
}

Horus Kol
02-15-2005, 09:04 AM
you need to delimit the string in the query:


$query = "SELECT * FROM pa_book WHERE ".$ways." LIKE '%".$keywords."%'";



and then it will only work if they only enter a single keyword - you need to allow for multiple search keys.

also, LIKE only finds strings with the comparison string in them...

if you want to allow for mistypes - like Google does - then you're going to have to somehow incorporate the following function:

http://uk2.php.net/manual/en/function.levenshtein.php