respecttheplayer Posted July 20, 2009 Share Posted July 20, 2009 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? Link to comment https://forums.phpfreaks.com/topic/166632-solved-if-query-does-not-exist-redirect-to-other-page/ Share on other sites More sharing options...
MatthewJ Posted July 20, 2009 Share Posted July 20, 2009 <?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"); } ?> Link to comment https://forums.phpfreaks.com/topic/166632-solved-if-query-does-not-exist-redirect-to-other-page/#findComment-878667 Share on other sites More sharing options...
rhodesa Posted July 20, 2009 Share Posted July 20, 2009 <?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']}"; } ?> Link to comment https://forums.phpfreaks.com/topic/166632-solved-if-query-does-not-exist-redirect-to-other-page/#findComment-878669 Share on other sites More sharing options...
respecttheplayer Posted July 20, 2009 Author Share Posted July 20, 2009 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! Link to comment https://forums.phpfreaks.com/topic/166632-solved-if-query-does-not-exist-redirect-to-other-page/#findComment-878672 Share on other sites More sharing options...
respecttheplayer Posted July 20, 2009 Author Share Posted July 20, 2009 quick reply...the one matthewJ did worked for me in case this forum gets searched in the future! Link to comment https://forums.phpfreaks.com/topic/166632-solved-if-query-does-not-exist-redirect-to-other-page/#findComment-878686 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.