Jump to content

[SOLVED] If query does not exist redirect to other page


Recommended Posts

My problem may be easier than what I am making it out to be but maybe you can tell me what I need to do or what I need to research to get this issue fixed. Basically I want to have a page that pulls a query from the database and if it does not exist to redirect to another page. My SQL is working flawlessly and is pulling the results but if a query does not exist there is private information that is still shown so I want it to be redirected rather than them staying on that page. My code is below

 

 

<?php
//Setup connection to the database 
$connect = mysql_pconnect("localhost", "user", "password") 
or die(mysql_error()); 

//Connect to the database 
mysql_select_db("userdatabase", $connect) or die(mysql_error());

$query  = "SELECT * from Employees WHERE email='$email' AND kv_code='$password'";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<b>Full Name:</b> {$row['full_name']}<br>
<b>Phone Number:</b> Main: {$row['phone_number']}";
} 

?>

 

 

I am then thinking that if the query does not exist or at least the query does not include any information to then redirect it using

header('Location: http://www.domain.com');

     

But where do I put the header redirect into the above where it is pulling the mysql result?

 

 

<?php
//Setup connection to the database 
$connect = mysql_pconnect("localhost", "user", "password") 
or die(mysql_error()); 

//Connect to the database 
mysql_select_db("userdatabase", $connect) or die(mysql_error());

$query  = "SELECT * from Employees WHERE email='$email' AND kv_code='$password'";
$result = mysql_query($query);
$totalrows = mysql_num_rows($result);
if($totalrows > 0) {
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<b>Full Name:</b> {$row['full_name']}<br>
<b>Phone Number:</b> Main: {$row['phone_number']}";
}
} else {
header("Location: www.domain.com");
}

?>

<?php
//Setup connection to the database 
$connect = mysql_pconnect("localhost", "user", "password") 
or die(mysql_error()); 

//Connect to the database 
mysql_select_db("userdatabase", $connect) or die(mysql_error());

$query  = "SELECT * from Employees WHERE email='$email' AND kv_code='$password'";
$result = mysql_query($query);
if(!$result){
  header('Location: error.php');
  exit;
}

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<b>Full Name:</b> {$row['full_name']}<br>
<b>Phone Number:</b> Main: {$row['phone_number']}";
} 

?>

awesome, thank you both for your quick replies, both look like they will work for what I was looking to do!!!! I appreciate the help!

 

I just was not sure if I was able to put conditional statements in between the $query, $result in fear that it might break the code and this will help me in lots of future coding!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.