PDA

View Full Version : Display error message if unique ID is not found


mike_jandreau
06-29-06, 01:10 PM
I've been lurking around the forum for a while, and finally come across something that I can't seem to find the answer for, either here, or via search.

I've done a fair job of learning how to do basic PHP/MySQL functions on my own thus far, and have created my site using the two, and the site is dynamic, which is good for what we're doing.

What I'm trying to figure out is how to display a certain error message when an item is called, but doesn't exist.

For example, I'm using this sql query: $query="SELECT * FROM reviews WHERE movieid='$movieid'";

To call info about our movie reviews from our database.

This results in URLs like review.php?movieid=xxxx

Works great. However, if someone calls a movieid that does not exist, the page will load partially (missing a lot of the content, and obviously no variables are echoed).

What I'm looking to do, is either redirect people who try to call a movieid that doesn't exist in my MySQL database to my 404 page, or simplay display empty variables on the page (but have it load completely)

Example of working page: http://www.moviesnobs.net/review.php?movieid=1545
Example of page with not-valid movieid: http://www.moviesnobs.net/review.php?movieid=500

Any ideas? Sorry this is such a long post, but I wanted to get my point across, and give as much info as possible.

Thanks to anyone who can help.

mab
06-29-06, 01:31 PM
I suspect that your existing code assumes there is always a result and when it echo's empty elements, it causes the page to be broken. Do something like the following -
$query="SELECT * FROM reviews WHERE movieid='$movieid'";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

if(mysql_num_rows($result) === 0)
{
// echo message that there is no movie that matched the request
} else {
// put your existing code here to display the results
}
// remainder of your codeEdit: Must have been by a few seconds...

Nico
06-29-06, 01:31 PM
Moved to database.

As for your question. It's quite simple. You can use mysql_num_rows() (http://us2.php.net/manual/en/function.mysql-num-rows.php).


$query="SELECT * FROM reviews WHERE movieid='$movieid'";
$result = mysql_query($query);

if (mysql_num_rows($result) < 1)
{
// Do whatever you want to do here if no result is found.
}



EDIT:

Ahh, Mab beats me this time. :p

mike_jandreau
06-30-06, 09:03 PM
Thanks for the help thus far guys. What you've given me so far, works great. However, I ran into another snag, and it's probably because of my lack of php knowledge.

Maybe you can help.

So, I took what you helped me with before, and added that to my page.

<?
$username="*********";
$password="********";
$database="********";

mysql_connect("*********",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM reviews WHERE movieid='$movieid'";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

$num=mysql_numrows($result);

mysql_close();

echo "";

//while ($i < $num) {
while ($row = mysql_fetch_array($result)){
$tags = $row['tags'];
$stars = $row['stars'];
$rating = $row['rating'];
$main_cast = $row['main_cast'];
$review = $row['review'];
$runtime = $row['runtime'];
$release_dvd = $row['release_dvd'];
$release_theater = $row['release_theater'];
$reviewed_by = $row['reviewed_by'];
$links = $row['links'];
$director = $row['director'];
$writer = $row['writer'];
$tags = $row['tags'];
$poster = $row['poster'];
$date_written = $row['date_written'];
$page_title = $row['page_title'];
$amazon = $row['amazon'];
$netflix = $row['netflix'];
$title = $row['title'];
}
?>

<?
if(mysql_num_rows($result) === 0)
{
echo "No Review Found"; // echo message that there is no movie that matched the request
}












else {
echo "<meta NAME=\"description\" CONTENT=\"Movie Snobs Movies Reviews by people who love movies\">
<meta NAME=\"keywords\" CONTENT=\"$tags\">
<meta name=\"revisit-after\" content=\"4days\">
<meta name=\"robots\" content=\"index,follow\">
<title>Movie Snobs - $page_title</title>
<script>

function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = \"\"
}
</script>
<script type=\"text/javascript\" src=\"http://www.moviesnobs.net/mover_script.js\"></script>
<link rel=\"stylesheet\" href=\"http://www.moviesnobs.net/style1.css\" type=\"text/css\" media=\"screen\">
<script>
function popupWindow(link, width, height) {
var trpopWin =

window.open(link,\"trpopWin\",\"toolbar=0,location=0,directories=0,status=0,menuba r=0,scrollbars=1,resizable-0,width=\"+width+\",height=\"+height);
}
</script></head>
<body marginwidth=\"0\" marginheight=\"0\" leftmargin=\"0\" topmargin=\"0\"

onload=\"Preload('images/bg_bottmenu.gif','images/bg_conbott.gif','images/bg_footer.gif','images/bg_head.gif','images/bg_menu.gif','images/bg_sidepanel.gif',

'images/bmenu_01.gif','images/bmenu_02.gif','images/bmenu_03.gif','images/bmenu_04.gif','images/bmenu_08.gif','images/box_01.gif','images/box_02.gif','images

/box_03.gif','images/box_04.gif','images/box_05.gif','images/box_06.gif','images/box_08.gif','images/box_09.gif','images/box_10.gif','images/box_11.gif','ima

ges/box_12.gif','images/box_14.gif','images/box_17.gif','images/bullet.gif','images/buttbs_back.gif','images/buttbs_back_sel.gif','images/buttbs_home.gif','i

mages/buttbs_home_sel.gif','images/buttbs_top.gif','images/buttbs_top_sel.gif','images/dfs_03.gif','images/dfs_08.gif','images/dfs_12.gif','images/logo.gif',

'images/pg_02.gif','images/pg_04.gif','images/pg_05.gif','images/pg_06.gif','images/pg_08.gif','images/pg_09.gif','images/pg_11.gif','images/spacer.gif','ima

ges/sub2_bullet.gif','images/sub_bullet.gif','images/subnav_bullet.gif','images/sub_bullet_dvd.gif','images/tmenu_01.gif','images/tmenu_02.gif','images/tmenu

_03.gif','images/tmenu_04.gif','images/tmenu_05.gif','images/tmenu_07.gif','images/tmenu_08.gif','images/tmenu_11.gif','images/tmenu_12.gif','images/tmenu_13

.gif','images/tmenu_14.gif','images/tmenu_head.gif')\"><a name=\"top\">
<table width=\"100%\" height=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td background=\"http://www.moviesnobs.net/images/bg_head.gif\" valign=\"top\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td width=\"291\" height=\"124\">
<table width=\"280\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>
<td><img src=\"http://www.moviesnobs.net/images/spacer.gif\" width=\"1\" height=\"1\"><img src=\"http://www.moviesnobs.net/images/tmenu_01.gif\" width=\"26\"

height=\"36\"></td>
<td><img src=\"http://www.moviesnobs.net/images/tmenu_02.gif\" width=\"78\" height=\"36\"><img src=\"http://www.moviesnobs.net/images/tmenu_03.gif\"

width=\"88\" height=\"36\"><img src=\"http://www.moviesnobs.net/images/tmenu_04.gif\" width=\"88\" height=\"36\"></td>

</tr><tr>
<td rowspan=\"3\"><img src=\"http://www.moviesnobs.net/images/spacer.gif\" width=\"1\" height=\"1\"><img

src=\"http://www.moviesnobs.net/images/tmenu_05.gif\" width=\"26\" height=\"64\"></td>
<td rowspan=\"3\"><img src=\"http://www.moviesnobs.net/images/tmenu_head.gif\" width=\"79\" height=\"64\"><img

src=\"http://www.moviesnobs.net/images/tmenu_07.gif\" width=\"88\" height=\"64\"><img src=\"http://www.moviesnobs.net/images/tmenu_08.gif\" width=\"88\"

height=\"64\"></td>

</tr><tr>
</tr><tr>

</tr><tr>
<td><img src=\"http://www.moviesnobs.net/images/spacer.gif\" width=\"1\" height=\"1\"><img src=\"http://www.moviesnobs.net/images/tmenu_11.gif\" width=\"26\"

height=\"24\"></td>
<td><img src=\"http://www.moviesnobs.net/images/tmenu_12.gif\" height=\"24\"><img src=\"http://www.moviesnobs.net/images/tmenu_13.gif\" width=\"88\"

height=\"24\"><img src=\"http://www.moviesnobs.net/images/tmenu_14.gif\" width=\"88\" height=\"24\"></td>

</tr>
</table>
</td>
<td width=\"716\" height=\"124\"><p align=\"right\"><script type=\"text/javascript\"><!--
google_ad_client = \"pub-6708600750793239\";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = \"468x60_as\";
google_ad_type = \"text\";
google_ad_channel =\"\";
google_color_border = \"CA5B00\";
google_color_link = \"CA5B00\";
google_color_bg = \"EEEEEE\";
google_color_text = \"000000\";
google_color_url = \"CA5B00\";
//--></script>
<script type=\"text/javascript\"
src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
</script>
</p>

</td>
</tr>
</table>
</td>
</tr>
<tr>
<td background=\"http://www.moviesnobs.net/images/bg_menu.gif\" valign=\"top\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td><img src=\"http://www.moviesnobs.net/images/spacer.gif\" width=\"1\" height=\"1\"><img src=\"http://www.moviesnobs.net/images/dfs_03.gif\" width=\"215\"

height=\"36\"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr></table></td>
</tr>
<tr>
<td height=\"100%\" valign=\"top\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td background=\"http://www.moviesnobs.net/images/bg_sidepanel.gif\" width=\"212\" height=\"100%\" align=\"center\" valign=\"top\">
<!-- Side Panel -->

<!-- News -->
<table width=\"198\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td>
<!-- Begin Reviews Box Box-->
<table width=\"198\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td><img src=\"http://www.moviesnobs.net/images/box_01.gif\" width=\"7\" height=\"12\"></td>
<td colspan=\"2\"><img src=\"http://www.moviesnobs.net/images/box_02.gif\" width=\"186\" height=\"12\"></td>
<td><img src=\"http://www.moviesnobs.net/images/box_03.gif\" width=\"5\" height=\"12\"></td>
</tr>
<tr>
<td><img src=\"http://www.moviesnobs.net/images/box_04.gif\" width=\"7\" height=\"21\"></td>
<td><img src=\"http://www.moviesnobs.net/images/box_05.gif\" width=\"30\" height=\"21\"></td>
<td background=\"http://www.moviesnobs.net/images/box_06.gif\" width=\"156\" height=\"21\" class=\"sub_title\"> Review Info </td>
<td><img src=\"http://www.moviesnobs.net/images/box_08.gif\" width=\"5\" height=\"21\"></td>
</tr>
<tr>
<td><img src=\"http://www.moviesnobs.net/images/box_09.gif\" width=\"7\" height=\"10\"></td>
<td colspan=\"2\"><img src=\"http://www.moviesnobs.net/images/box_10.gif\" width=\"186\" height=\"10\"></td>
<td><img src=\"http://www.moviesnobs.net/images/box_11.gif\" width=\"5\" height=\"10\"></td>
</tr>
</table></td>
</tr>
<tr>
<td><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td><img src=\"http://www.moviesnobs.net/images/box_12.gif\" width=\"7\" height=\"100%\"></td>
<td bgcolor=\"#FAFAFA\" width=\"186\" height=\"43\" valign=\"top\">
<b>Movie Title:</b> $title<br><b>Director:</b> $director<br><b>Written by:</b> $writer<br> <b>Main Cast:</b> $main_cast<br><b>MPAA Rating:</b>

$rating<br><b>Runtime:</b> $runtime<br> <b>Reviewed by: </b> $reviewed_by<br>
<B>Snob-Rating:</b><br><center><img src=\"http://www.moviesnobs.net/images/stars/$stars.gif\" alt=\"Rating\"></center>
<hr width=\"100%\" size=\"1\" color=\"#B2B2B2\" noshade><br><b>Release Date:</b> $release_theater<br><b>DVD Release Date:</b> $release_dvd<br><img

src=\"http://www.moviesnobs.net/images/sub_bullet.gif\" alt=\"\"> <a href=\"http://www.netflix.com/AddToQueue?movieid=$netflix\" target=\"_blank\"

onmouseover=\"window.status='';return true\" onmouseout=\"window.status='';return true\" target=\"_blank\" title=\"Add $title To Your Netflix Queue\">Netflix

$title</a><Br><img src=\"http://www.moviesnobs.net/images/sub_bullet.gif\" alt=\"\"> <a href=\"$amazon\" title=\"Buy $title at Amazon.com\" target=\"_blank\"

onmouseover=\"window.status='';return true\" onmouseout=\"window.status='';return true\">Buy $title at Amazon</a><img

src=\"http://www.assoc-amazon.com/e/ir?t=moviesnobs-20&amp;l=ur2&amp;o=1\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important;

margin:0px !important;\" /><br>

</td>
<td><img src=\"http://www.moviesnobs.net/images/box_14.gif\" width=\"5\" height=\"100%\"></td>
</tr>
</table></td>
</tr>
<tr>
<td><img src=\"http://www.moviesnobs.net/images/box_17.gif\" width=\"198\" height=\"8\"></td>
</tr>
</table>
<!-- End Reviews Box Box --><? include('boxes/boxes.php') ?>
<p>&nbsp;</p></td>
<td width=\"785\" height=\"339\" valign=\"top\">

<table height=\"100%\" border=\"0\" cellpadding=\"6\" cellspacing=\"0\" width=\"768\"><tr>
<p>&nbsp;<br> <!-- Begin Body Here -->



<center><span class=\"castlarge\">$title</span><br>
Review written on $date_written<br><br><img src=\"http://www.moviesnobs.net/images/content/email_send.gif\" alt=\"\"> <a href=\"refer_a_review.php\"

onmouseover=\"window.status='';return true\" onmouseout=\"window.status='';return true\" target=\"_blank\" title=\"Send this review to a friend!\">E-mail

this review to a friend</a> <img src=\"http://www.moviesnobs.net/images/content/printer.gif\" alt=\"\"> <a href=\"print_view.php?movieid=$movieid\"

target=\"_blank\" title=\"Printable Version\">Printable View</a></center><Br>
<blockquote><blockquote><img src=\"http://www.moviesnobs.net/images/posters/$poster\" border=\"2\" align=\"right\" hspace=\"15\" vspace=\"5\"

alt=\"$page_title\">$review<p><B>See more reviews for the main cast of $title</b>: $main_cast</blockquote></blockquote>

<!--End Body -->
</td></tr></table>
</td>
</tr>
</table>

<tr>
<td background=\"http://www.moviesnobs.net/images/bg_conbott.gif\" valign=\"top\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td width=\"28\"><img src=\"http://www.moviesnobs.net/images/bmenu_01.gif\" width=\"28\" height=\"37\"></td>
<td width=\"24\"><img src=\"http://www.moviesnobs.net/images/bmenu_02.gif\" width=\"24\" height=\"37\"></td>
<td width=\"22\"><img src=\"http://www.moviesnobs.net/images/bmenu_03.gif\" width=\"22\" height=\"37\"></td>
<td width=\"66\"><img src=\"http://www.moviesnobs.net/images/bmenu_04.gif\" width=\"66\" height=\"37\"></td>
<td width=\"75\"><img src=\"http://www.moviesnobs.net/images/dfs_12.gif\" width=\"75\" height=\"37\"></td>
<td width=\"788\" height=\"37\"></td>
</tr>
</table></td>
</tr>
<tr>
<td background=\"http://www.moviesnobs.net/images/bg_bottmenu.gif\" valign=\"top\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td><a href=\"http://www.moviesnobs.net/index.php\" onmouseover=\"on8();\" onmouseout=\"off8();\"><img

src=\"http://www.moviesnobs.net/images/buttbs_home.gif\" width=\"28\" height=\"24\" border=\"0\" name=\"h\" onmouseover=\"window.status='Home Page';return

true\" onmouseout=\"window.status='';return true\"></a></td>
<td><a href=\"javascript:history.back(1)\" onmouseover=\"on9();\" onmouseout=\"off9();\"><img src=\"http://www.moviesnobs.net/images/buttbs_back.gif\"

width=\"24\" height=\"24\" border=\"0\" name=\"i\" onmouseover=\"window.status='Back A Page';return true\" onmouseout=\"window.status='';return

true\"></a></td>
<td><a href=\"#top\" onmouseover=\"on10();\" onmouseout=\"off10();\"><img src=\"http://www.moviesnobs.net/images/buttbs_top.gif\" width=\"22\" height=\"24\"

border=\"0\" name=\"j\" onmouseover=\"window.status='Top Of Page';return true\" onmouseout=\"window.status='';return true\"></a></td>
<td><img src=\"http://www.moviesnobs.net/images/bmenu_08.gif\" width=\"66\" height=\"24\" border=\"0\"></td>

</tr>
</tr>
</table></td>
</tr>
<tr>
<td background=\"http://www.moviesnobs.net/images/bg_footer.gif\" valign=\"top\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td><img src=\"http://www.moviesnobs.net/images/spacer.gif\" width=\"770\" height=\"40\"></td>
</tr>
</table></td>
</tr>
</table>
<? include(\"footer.php\") ?>
</body>
</html>";

// put your existing code here to display the results
}


echo "";
?>

However, my <? include tags, which used to work (when I wasn't echoing the entire page), don't work anymore.

Am I missing something, or am I going about this all wrong? Should I be echoing the entire thing in one bang like that? Or is that what's causing the two "includes" to not work?

Thanks again for the help.