title: Using PHP to redirect when SQL query comes up empty
body: <p>I am using this PHP script to return search input values with corresponding URL values on a MySQL database/table. The idea is to append them to a redirect to automatically jump to that page.  </p>
  
  <pre><code>&lt;?php
  $searchResults = $_POST['search'];
  $dbhost = 'localhost';
  $dbuser = 'admin';
  $dbpass = 'pwd';
  $conn = mysql_connect($dbhost, $dbuser, $dbpass);
  if(! $conn )
  {
    die('Could not connect: ' . mysql_error());
  }
  $sql = "SELECT url
      FROM Table_1
      WHERE input = '" .$searchResults."'";
  
  mysql_select_db('database_1');
  $retval = mysql_query( $sql, $conn );
  if(! $retval )
  {
    die('Could not get data: ' . mysql_error());
  }
  while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
  {
      $redirect = $row['url'];
      header('Location:'.$redirect);  
  } 
  </code></pre>
  
  <p>To catch any input that does not match a value on Table_1 added this If statement.  It will take any non relevant or misspelled inputs and redirect them to xyz.html</p>
  
  <pre><code>if (mysql_num_rows($retval) &lt; 1)  {
  
  header('Location: xyz.html');
  
  }
  </code></pre>
  
  <p><strong>Is this incorrect?</strong>  It seems to be working but I assume there must be a cleaner way of doing this/ it may be <strong>bad practice.</strong></p>