I have this database connection code:
$host = 'localhost';
$db = 'cssnewlogin';
$user = 'root';
$pass = '';
$charset = 'utf8';
//We do the connection:
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $user, $pass, $opt);
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, FALSE);
And I execute a simple INSERT:
$sthinsert = $pdo->prepare(
"INSERT INTO lithuania_customers_2016
(user_id, customer_db_id, campaign_id)
VALUES (:user_id, :customer_db_id, :campaign_id)"
);
$sthinsert->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$sthinsert->bindValue(':customer_db_id', $customer_db_id, PDO::PARAM_INT);
$sthinsert->bindValue(':campaign_id', $campaign_id, PDO::PARAM_INT);
$sthinsert->execute();
At what point do I have to do a die();
so that after executing the desired code there are no other unexpected behaviors?
In this case the answer is never .
There does not have to be unexpected behavior, unless after
INSERT
you try to do something else.Even the PDO connection does not need to be closed, although you can if you wish. If you don't close it, PHP closes it when the script ends.
From the PHP manual :
More details in this answer: Is it necessary to close a connection with PDO after executing a statement?
You don't have to fill everything from
dies()
everywhere, but to control your code. If, for example, theINSERT
is inside a loop or inside a condition,if then else
check that the evaluation is done correctly and that the code goes through where you want it to go, without staying in an infinite loop or something like that.EDIT:
What you do need to worry about is verifying that you
execute
were successful or not, by doing something like this:If it was successful
$resultado
it will be true (TRUE) and you can put a message indicating it inside the if or pass to another action, if it was not successful you can add an else to indicate it.Short answer: never . A die() or exit() is not the natural way to end the flow of a script. The correct thing would be to use an exception, either when creating the connection or when executing the statement. A minimal example would be:
Long answer:
Once upon a time, in a galaxy far, far away, there was the practice of "try to connect or die".
What does this mean? That an error in
mysql_connect
is silently ignored. It receives a null or a boolean FALSE and that one has to explicitly ask what the last error was.With the advent of Test Driven Development it became clear that this is a bad practice: it is impossible to test. If the expected behavior were:
It is impossible to pass that test, because by doing
die
orexit
you also kill the test runner.Although testing is way out of the scope of this question, the important thing is that PDO is built to handle exceptions. It's in its nature that you don't need to cast errors to exceptions, or ask for the last error. What's more: PDO implements its own type of Exceptions ( PDOException ) in case you want to catch different classes of exceptions at a higher scope.
How to take advantage of this? You simply prepare for an exception.
In your same example you use that option:
Then instead of just doing (as in your example)
You should do (to take advantage of exceptions):
The nice thing about exceptions is that they propagate upwards, so you could also do:
And so on. Any PDO statement can be wrapped in a try/catch, or else an exception will propagate to the try/catch it finds in a higher scope.
Going back to the case of Test Driven Development, instead of killing the script along with the test runner, you can say:
An exception is perfectly testable and allows you to continue the flow of your script by modifying its behavior until it delivers a response to the user.
In addition, an exception includes the $e->getTrace() and $e->getTraceAsString() methods that let you know exactly where the exception occurred, where that part of the code was called from, and where that part of the code was called from. from where it was called (and so on until I think the tenth predecessor).
In an application of medium complexity, it would be very difficult to give each
die()
one a message that clearly indicates what the error was and where it occurred. For example, 10 different scripts may call the function that creates the connection. With adie()
it is not trivial to know which of those 10 is the one that generated the failure.If I have understood you correctly,
die();
you should put it after the line$sthinsert->execute();
Thus after the execution of the
INSERT
does not continue executing anything else.