Jump to content

[SOLVED] Sending 404 error header if MySQL query doesn't return results


Recommended Posts

So I have a script that is meant to return only one record (it's a product view page). Right now if you try and pass in a product ID that doesn't exist, the page loads the layout structure but without all the data. This isn't usable to me so I want to throw a 404 error if they try and pass an invalid ID number.

 

Problem is, in Firebug when I deliberately load the product viewing page with a false ID, it gives back header code 200 OK. I would like search engines to see 404 error pages if they come across invalid product / document ID's so they can be removed from their indexes.

 

I understand some of this would need to edit Apache's config file, but I'm unsure what needs to be edited. Any help is greatly appreciated. Here's the code I'm currently using:

$read_product="SELECT * FROM products WHERE control = '".$id."'";
$product = mysql_query($read_product);
if (mysql_num_rows($product) < 1)
{
header("HTTP/1.0 404 Not Found");
include("/home/beta/public_html/404.php");
exit;
} else {
$data = mysql_fetch_array($product);
}

The above should work without any modification...why it is not sending the error I do not know. Are you sure there is no output before the header tag is called?

 

Yeah, there's no whitespace outside of the PHP tags - which as I understood it would cause a header error. All the stuff in the PHP tags just format data to send to the HTML flow. The very first part of the PHP code just grabs the ID from the URL and queries the DB.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.