Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with search engine 1

Status
Not open for further replies.

bentleykf

Programmer
Apr 29, 2002
59
AU
I seem to be having problems with my search engine when a search is performed and no results are found. What I've been trying to do is to display a message saying "Sorry, no records found" when no record is found, but instead the page displays nothing. I think it might be a problem with the "if" statements, though I'm not sure. Can anybody help me?

Search script
-----------------------------------------
$db = mysql_connect("localhost","username","password") or die ("Could not connect to server");
mysql_select_db("username_db",$db) or die ("Could not connect to database");
$query = "SELECT * FROM celebdb WHERE search LIKE '%,$search,%' OR search LIKE '$search,%' OR search LIKE '%,$search'";
$result = mysql_query($query,$db);

if(!$result) {
printf(&quot;<tr><td bgcolor=aabbe5 class=contentbl>Sorry, no records found</td></tr>&quot;);
} else {
while ($myrow = mysql_fetch_array($result)) {
printf(&quot;<tr><td bgcolor=aabbe5 width=108 class=contentbl><a href=%s><img src=%s width=200 height=150 vspace=4 hspace=3></a></td><td bgcolor=bbccf5 class=contentbl width=390 valign=top height=85>&nbsp;&nbsp;<b>Name:</b>&nbsp;<a href=%s>%s</a><br><b>DOB:</b>&nbsp;%s<br><b>Feild:</b>&nbsp;%s<br><b>------------------------------------------------------</b><br><img src=../_images/spacer.gif width=30 height=5>%s</td></tr>&quot;, $myrow[&quot;fileloc&quot;], $myrow[&quot;picloc&quot;], $myrow[&quot;fileloc&quot;], $myrow[&quot;name&quot;], $myrow[&quot;dob&quot;], $myrow[&quot;feild&quot;], $myrow[&quot;desc&quot;]);
 
I thinks its your SQL query where you have commas after the % signs for stuff to search for :
$query = &quot;SELECT * FROM celebdb WHERE search LIKE '%,$search,%' OR search LIKE '$search,%' OR search LIKE '%,$search'&quot;;

when you should have it like '%$search%' ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Try getting a count of records found :
$rows=mysql_num_rows($result);
if($rows > 0 ){
show results
} else {
no records found
}

$result will return a value of 0 or more so you cannot sucessfully test it with isset();
!empty() can still produce odd results too. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top