Search the Community
Showing results for tags 'exception'.
-
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.
- 4 replies
-
- exception
- db exception
-
(and 1 more)
Tagged with:
-
I am trying to finish up the programming work for my site's integration with a payment processor called Stripe. The last step is catching HTTP error response codes that happen during the customer creation process. In the code below, the exception will get throw when "Stripe_Customer::create" is reached if the credit card is declined or a sleuth of other things. What I want to do is take the Exception message and pass it to Smarty. Can I not do this in the Exception block? Right now, the code in the Exception block is throwing a Server Error. If I take out the code and just do a "echo $e->getMessage;", then it works just fine. Any help would be appreciated! try { $customer = Stripe_Customer::create(array( "description" => es($_POST['fname']) . " " . es($_POST['lname']), "email" => es($_POST['e-mail_add']), "card" => es($_POST['stripeToken']) )); // Validation information for card that was entered. $cvcCheck = $customer->active_card->cvc_check; $addressCheck = $customer->active_card->address_line1_check; $zipCheck = $customer->active_card->address_zip_check; // Validation checks for the card that was entered. if($cvcCheck == 'fail'){ $error = true; $smarty->assign("cvcError", true); } //If both the address and zip check failed or are left unchecked, //then the card cannot be charged properly and an error should be //displayed to the user. //If only one of them fails, then the charge can still process through //as long as the CvC is correct. if(!$error && $addressCheck != 'pass' && $zipCheck != 'pass'){ $error = true; $smarty->assign("addressError", true); } //Deleting the temporary customer. $cu = Stripe_Customer::retrieve($customer->id); $cu->delete(); } catch (Exception $e){ $errorMessage = $e->getMessage(); $smarty->assign("cardError", true); $smarty->assign("cardErrorMessage", $errorMessage); }