On my MySQL search what I want to do is redirect a user to another page and show an error message if the search returned no results. But for some reason the user never gets redirected.

Can you spot my problem? :|

$sql = "SELECT * FROM `list` WHERE `title` LIKE '%".$search[0]."%' AND `url` LIKE '%".$sites[0]."%'";
		
$catcnt = mysql_fetch_row(mysql_query($sql));

if($catcnt[0] == ''){header('Location: index.php');}else{echo $catcnt[0];}

Recommended Answers

All 6 Replies

you don't echo a header redirect

Ooops. Thats a mistype. The problem still stands though. Ill update the code in the first thread.

Also it would probably be better to use strlen() or empty() to determine if there is no value to the array. Script should look like following:

$sql = "SELECT * FROM `list` WHERE `title` LIKE '%".$search[0]."%' AND `url` LIKE '%".$sites[0]."%'";
 
$catcnt = mysql_fetch_row(mysql_query($sql));
 
if(empty($catcnt[0]))
{ 
header('Location: index.php');
}
else{
echo $catcnt[0];
}

If you search the forum first you can often find previous posts with similar problems. Try this one as an example:
http://www.daniweb.com/forums/thread101948.html

Thanks for that...

I needed to use javascript for this. Heres my code now:

<?php
 if(empty($catcnt)){
echo("<script>location.href = 'index.php?error=Your search returned no results';</script>");
}else
{echo $catcnt[0];
} 
?>
$sql = "SELECT * FROM `list` WHERE `title` LIKE '%".$search[0]."%' AND `url` LIKE '%".$sites[0]."%'";
       
    $catcnt = mysql_fetch_row(mysql_query($sql));
         
    if(@!isset($catcnt[0]) && $catcnt[0]=='')
   {
         header('Location: index.php');
         exit;
   }
   else{
       echo $catcnt[0];
  }

-- Always use exit while using "header";

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.