I am creating scripts using traditional mysql functions and though to shift to PDO. I searched google and I am reading so many articles on PDO on the internet and all are telling same thing. Like some article guy is copy/pasting articles for every dev. site.
Anyways the question is that all say that for error handling you do: try and catch. Okay but do I do that for every query ??? I have like 10 queries in a single php script and if I do try / catch for all those 10 queries then it will so messed up.
Now in the above code only 1 query is being executed. What if i have to run 3 more queries do i put that code 3 more times ??? No wonder people are using old mysql code still. I have checked so many sites and no one is giving real world PDO example. They just tell how to get single record, how to add etc. but no REAL WORLD sample.
[list=1]
[]You don’t need to constantly redefine your connection ($conn) if you have it in your config.php file. So leave that inside the try/catch of your config and remove the one outside your try/catch. Then remove all $conn assignments in your index.php (along with the $conn->setAttribute)
[]That should leave your index.php queries to start with the $stmt lines. If you need to run multiple queries, do them all in the same try/catch (if you can, more on that latter)[/list]
The first thing you need to really grasp is the concept of Exceptions and what they mean to your application. The thing you need to realize is by catching an exception you are allowing your program to continue executing (potentially, unless you call die() or something similar in the catch).
So you first need to ask yourself, can my program successfully continue to run if this query fails? If your answer is yes, then you should put it in a try/catch and do whatever alternate path you want to take in your catch so the program will continue to run properly. If your answer is no, your try/catch is useless, just let the exception bomb the page on its’ own (the only refutal to this statement is if you wanted to log the exception in a specific way, you’d still want the try/catch, the catch will have logic to log the exception and then you can use throw to cause the exception to bubble up or you can use die to kill the processing immediately).
Thanks for your response. Now atleast I am starting correctly / going in correct direction.
Regarding the ques: “can my program successfully continue to run if this query fails?”
My answer is no, but i do not want the default php error to show up. Like we used to do die(mysql_error()) i would like some specific message to show up that is specified by me for each query when it fails so I know what has failed and what to check / fix.
In addition, you may want to look at a logger, like Monolog. The nice thing about Monolog is that you can log to multiple outputs. For example, at work we log normal information to MongoDB, warnings and errors go to MongoDB as well, but fatal errors trigger a Pushover message to be sent to the devs so we can investigate. Works very well, I’m very happy with it!
Thanks by using that I am able to centralized the errors, but if I have say 3 different queries in a single php page and 1st executed fine and 2nd has error then I will get the generic error. How to throw custom error for that specific query so I quickly know what failed and where to check ? And now my code looks like this:
class ApplicationException extends Exception
{
protected $applicationMessage;
public function __construct($exception, $applicationMessage)
{
$this->applicationMessage = $applicationMessage;
parent::_construct($exception->getMessage(), $exception->getCode(), $exception);
}
public function getApplicationMessage()
{
return $this->applicationMessage;
}
}
Then in your catch
catch (PDOException $e)
{
throw new ApplicationException($e, "My custom message");
}
In your exception handler
function exception_handler($exception) {
echo "Uncaught exception: " , $exception->getMessage(), "\
";
if ($exception is ApplicationException)
die($exception->getApplicationMessage());
else
die("Oops! Something bad happened");
}
All of that is untested and done from memory (there could be a better way, I’m just reliving things I know I’ve seen in .NET and applying them to PHP).
As for your second question: bindParam will sanitize the data for you. However, you may want to read the following url so you understand the differences between bindParam and bindValue (read the answer from lonesomeday, which has a good example). http://stackoverflow.com/questions/1179874/pdo-bindparam-versus-bindvalue