title: PHP Page redirect problem - Cannot modify header information
body: <p>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).</p>
  
  <p>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!</p>
  
  <pre><code>&lt;?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)) { 
  </code></pre>
  
  <p>The following modification as kindly suggested by <a href="https://stackoverflow.com/users/823942/matt-wilson">Matt Wilson</a> as a result of an original comment by 
  <a href="https://stackoverflow.com/users/428705/vivek-goel">Vivek Goel</a> results in valid entries showing the page correctly but non-existent pages are showing the errors below this modified code: </p>
  
  <pre><code>&lt;?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)) { 
  </code></pre>
  
  <p>Errors resulting from the above modifications: </p>
  
  <pre><code>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
  </code></pre>
  
  <p>Lines 22 and 23 are the two header lines as below:</p>
  
  <pre><code>header("HTTP/1.1 301 Moved Permanently");
  header( 'Location: http://examplesite.domain/errorpage' ) ;
  </code></pre>