1

I don't think I explained myself adequately in my previous question.

I have a page that displays various elements even if the id it's calling from the database does not exist or was deleted (which throws up all sorts of ugly errors along with search engines continuing to list non-existent pages).

Can you modify the first part of the page code shown below to send a 404 (or at least to projecterror.php which has 404 headers) if $id does not exist? Many thanks!

<?php
include_once("includes/linkmysql.php");
$adda=$_GET['a']; 
$cont=$_GET['c']; 
$select="SELECT * FROM projects where id='$id'";
$qselect = mysql_query($select);
while ($row = mysql_fetch_array($qselect)) { 
RiaD
  • 46,822
  • 11
  • 79
  • 123
JoeW
  • 578
  • 1
  • 10
  • 29
  • possible duplicate of [Redirect page when database id is not matched](http://stackoverflow.com/questions/6974691/redirect-page-when-database-id-is-not-matched) – David Aug 07 '11 at 20:24
  • If you want to clarify your previous question, feel free to edit it. Someone there was already trying to help you. – David Aug 07 '11 at 20:24
  • OK, I didn't realise editing was possible, thanks for the heads up. – JoeW Aug 07 '11 at 20:39
  • Original post has been edited. – JoeW Aug 07 '11 at 21:11

4 Answers4

2

Since only relying on HTTP error codes sometimes can be a little too ambiguous, I suggest you include the error in an XML/JSON response message in addition to the general HTTP status code. That way you can provide better logging and since the error is stated in detail in the message it will drastically shorten debugging time / provide the opportunity for you to present the source of the error more clearly.

Nirma
  • 5,640
  • 4
  • 35
  • 47
1
<?php
include_once("includes/linkmysql.php");
$adda=$_GET['a']; 
$cont=$_GET['c']; 
$select="SELECT * FROM projects where id='$id'";
$qselect = mysql_query($select);
if( mysql_num_rows( $qselect ) === 0 )
{
   header("HTTP/1.1 301 Moved Permanently");
   header( 'Location: http://examplesite.domain/errorpage' ) ;
   exit;
}
while ($row = mysql_fetch_array($qselect)) { 

These are the header codes @Vivek Goel gave you on the other questions you asked

Matt R. Wilson
  • 7,268
  • 5
  • 32
  • 48
  • This solution results in valid entries showing the page correctly but non-existent pages are showing the following errors: `Warning: Cannot modify header information - headers already sent by (output started at /home/website/public_html/header1.php:14) in /home/website/public_html/header1.php on line 22` `Warning: Cannot modify header information - headers already sent by (output started at /home/website/public_html/header1.php:14) in /home/website/public_html/header1.php on line 23` Lines 22 and 23 are the two header lines in your example above. – JoeW Aug 07 '11 at 20:52
  • 2
    This needs to be higher up in your code. Headers need to be sent before any other information, such as raw text or echos. – Matt R. Wilson Aug 07 '11 at 21:08
  • The above code is at the top of the header.php page, the first thing called to be displayed. I can't figure this out! – JoeW Aug 07 '11 at 22:56
0

No one in the above answers have answered it in MySQLI for Mysqli use the below code if a record is not found in the database

<?php
ob_start();
include_once("includes/config.php")//mysqli config;
$adda=$_GET['a']; 
$cont=$_GET['c']; 
$select="SELECT * FROM projects where id='$id'";
$result = $conn->query($select) or die("Cannot write");
$numrows = $result->num_rows;
if( $numrows === 0 )
{
   ob_end_clean();
   header("HTTP/1.1 301 Moved Permanently");
   header( 'Location: http://examplesite.domain/errorpage' ) ;
   exit();
}
while ($row = mysql_fetch_array($qselect)) {

regards www.psychocodes.in

0
include_once("includes/linkmysql.php");
$adda=$_GET['a']; 
$cont=$_GET['c']; 
$select="SELECT * FROM projects where id='$id'";
$qselect = mysql_query($select);
if(!mysql_num_rows($qselect)){
    header($_SERVER['HTTP_PROTOCOL']. '404 Entry Not Found');
    //print other info
}
else{
 //do usual code

}

RiaD
  • 46,822
  • 11
  • 79
  • 123