I am very new to php & mysql. i have made a webste with a database in mysql. i have developed a search engine to search my database.
I just discovered a weakness in my current search engine. It won't show results if I don't have an exact match
or
if I use two or more words that appear in different fields of the table.
For example, search term 'John' appears in field[wname] and 'canada' appears in field[country]. But when I type 'john canada' into my search form I get no results - unless I only use one or the other in the search form. It also does not work if I type in any extra words whether they are in the table or not.
I need to search multiple fields without having exact matches
if you can help me i would be very greateful to you. i am a librarian working in Saudi Arabia. i will send my seach script to you here. if you can look in to it and give me some instruction i will appreciate.
thank you in advance
this is my script.
<?php
$link = mysql_connect('localhost', 'root', 'vipula');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('test1');
@mysql_select_db(test1) or die("unable to connect");
$choice=$_POST;
$name=$_POST;
$query="SELECT * FROM staffpub WHERE";
$query.=" authors LIKE '%$name%'";
$query.=" OR title LIKE '%$name%'";
$query.=" OR source LIKE '%$name%' ORDER BY title";
$result=mysql_db_query("test1", $query);
$num_rows = mysql_num_rows($result);
if ($num_rows==0){$search_result="<p class='error'>No Results Found</p>";}
elseif ($num_rows > 0){$search_result="<p class='error'>".$num_rows. " Results Found</p>";}
mysql_close();
echo '<b><center><font size="4" color="#FF0000">Search Result</font></center></b><br><br>';
$i=0;
while ($i < $num_rows) {
$authors=mysql_result($result,$i,"authors");
$title=mysql_result($result,$i,"title");
$source=mysql_result($result,$i,"source");
echo "<b>Authors:</b> $authors<br><b>Title:</b> $title<br><b>Source:</b> $source<br><br><hr><br>";
$i++;
}
?>