if no results from search echo message

This is my first PHP/MYSQL attempt.
All is going well I have a database with sales reps and zip codes they rep.
If you do a search for the zip code in the DB it works perfect.

What I want to do though is if the user searches for a Zip not in my database I want to echo a message, for example no results found or link them to a page.

If anyone can help me setup a if/else for no results it would be appreciated. Tried several things, but no luck.

Here is the code that works for the search if Zip exists:

[php]<?php
mysql_connect (“localhost”, “peterson_galiant”,“p3t3rs0n”) or die (mysql_error());
mysql_select_db (“peterson_zipcode”);
$term = $_POST[‘term’];
$sql = mysql_query(“select * from reps_zip where zip like ‘%$term%’”);
while ($row = mysql_fetch_array($sql)){
echo ‘’;
echo ‘
’.$row[‘name’];
echo ’
Contact Info’;
echo ‘

’;
}
?>[/php]

this will do what you need

[php]

<?php mysql_connect ("localhost", "peterson_galiant","p3t3rs0n") or die (mysql_error()); mysql_select_db ("peterson_zipcode"); $term = $_POST['term']; $sql = mysql_query("select * from reps_zip where zip like '%$term%'"); $num = mysql_num_rows($sql); if($num == "0") { //what to do if no results found } else { while ($row = mysql_fetch_array($sql)){ echo ''; echo '
'.$row['name']; echo '
Contact Info'; echo '

'; } } ?>

[/php]

Worked Perfectly… THanks.
I was close but not getting there.

How would I make the no result redirect to a new or existing page on the site?

sorry for the late reply, you need to use header(), make sure there is no output to the user before the redirection (like an echo)

[php]
header(‘Location: http://www.yoursite.com’);
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service