Forum Moderators: coopster
When a page is called i.e example.com/page?id=1 I run a select on the MYSQL database:
SELECT id FROM categories WHERE id = '$id'
If $id doesn't exist I want to direct to a 404 page instead of just using the echo as I have now:
if (empty($id) {
echo 'error no ID - I sure wish you were seeing a 404 page here instead.';
exit;
My .htaccess has the error document line and works fine, I just can't figure out what code to use instead of echo.
Could anyone help me out?
$result = mysql_query($query);
if(mysql_num_rows($result) == 0) {
[url=http://us3.php.net/header]header[/url]("HTTP/1.0 404 Not Found");
}
Also, make sure to escape your query variables:
$id = [url=http://us3.php.net/manual/en/function.mysql-real-escape-string.php]mysql_real_escape_string[/url]($id);
this should work
only there cannot be any headers sent before, or use Output buffering [de3.php.net]
Hope this helps you.
Michal
2 minutes lag ;)
includeto display your custom 404 page.
I include the
header ("HTTP/1.0 404 Not Found"); in my custom 404 page so I don't have to remember that bit whenever I am returning a 404 to the visitor. :)
WW, your help has been much appreciated so far.
Now I am trying to figure out which IF statement is most practical in this situation.
if (empty($ID)){
if(mysql_num_rows($row) == 0) {
Both work fine - is there any difference besides them being different languages? Is it just preference?
Cheers.
(edit: $t was written instead of $ID - too many hours infront of the PC I guess.)
[edited by: scraptoft at 7:48 pm (utc) on Jan. 24, 2007]
if (empty($ID)){
And this:
if(mysql_num_rows($row) == 0) {
Are actually doing two different things. The first one is checking to see whether or not $_GET['id'] is set in the URI query.
The second is actually running the $_GET['id'] variable through a query to see if it exists in the database table. If I was you, I'd have both. This way it will give a NOT FOUND for an empty id value and if the id is defined, but just doesn't exist in the table.
Good luck :)