ajoo Posted June 15, 2015 Share Posted June 15, 2015 (edited) Hi all,I have a snippet of code below. It connects to a DB and then calls a function findMail() if all is well. Code: <?php $host = 'localhost'; $user = 'root'; $password = ''; $database = 'test'; // connect try { $con = new mysqli($host,$user,$password,$database); if($con->connect_errno > 0) throw new Exception("Server goof up!"); mysqli_set_charset($con, "utf8"); } catch(Exception $e){ $e->getMessage(); } if(findMail($con)) echo "<br> Hurray !!"; function findMail($con) { try { $query = "SELECT name, email from users"; $stmt=$con->prepare($query); throw new exception("Problem in DB"); if($stmt->execute()) { $stmt->bind_result($name, $email); while ($stmt->fetch()) { $email = htmlspecialchars($email); $name = htmlspecialchars($name); echo $name.' ---- '.$email.'<br>'; } } } catch(Exception $e){ $e->getMessage(); } return true; } ?> The function findMail() executes a query and displays the name and email from the DB, returns and prints Hurray. If there is an exception thrown in the handling of the DB within the function, then the names and emails from the DB are not echoed, the function returns and prints only Hurray!.Now if it is critical that findMail() executes successfully for the program to proceed further. ( print hurray on exiting the function) i.e it is important for the call to DataBase not fail, then how should this exception be handled by the program to gracefully exit the program then and there. ( Not print hurray). Kindly explain by extending the snippet above. Would this be an ideal case for making a call to an error page ( such as 404) on exit to inform the cliet to try again later maybe?Also how can we ensure that any attempts to reload the previous page using a back key be foiled.Thanks loads everyone. Edited June 15, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted June 15, 2015 Share Posted June 15, 2015 You can use the header() function to do any error handling or redirection. Quote Link to comment Share on other sites More sharing options...
ajoo Posted June 15, 2015 Author Share Posted June 15, 2015 Hi , Thanks for the response. Kindly also explain how can I get the code to discontinue execution once an exception occurs. In the snippet, once the exception is thrown in the function, the code after that in that block function is not executed but once back in the main it goes on to echo "Hurray". This is what I want to avoid. So once the exception occurs, I want the code terminated totally, a message displayed to the user on a nice page ( through redirection as you suggested.) Further is it possible to ensure that the user cannot return to the previous page through the backspace or browser buttons. Thanks very much ! Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted June 15, 2015 Share Posted June 15, 2015 Do an exit() to stop continuation of the code Quote Link to comment Share on other sites More sharing options...
ajoo Posted June 16, 2015 Author Share Posted June 16, 2015 Hi QuickOldCar, Thanks once again for the response. I was looking for an affirmation on using the exit and header functions since I read that using die(), exit() and header() was bad form and should be avoided and replaced by none else than an exceptions handler. But I guess to terminate as I mentioned I would need to use either exit or die as suggested by you. Thanks very much. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.