I have a URL shortener script that makes the user provide the record ID then redirects them. However, I want to be able to redirect the user to a specific page if the record they searched for does not exist. How can I do this?
My current code is:
<?
mysql_connect("localhost","username","password");
mysql_select_db("database");
$sql="select * from shortened where (shortened.id='".mysql_real_escape_string($_GET['url'])."')";
$query=mysql_query($sql);
while($row = mysql_fetch_array($query)){
header('Location:'.$row['url']);
}
?>
I hope people can understand what I’m trying to describe.
Put
right before your
whilepartAnd your
whilecould be actually rewritten as:(yes, just remove
while– it is pointless in this case)