I havethe following code…
// Verify Update.
if (mysqli_stmt_affected_rows($stmt2)==1){
// Update Succeeded.
}else{
// Update Failed.
$_SESSION['resultsCode'] = 'PASSWORD_RESET_FAILED';
// Redirect to Display Outcome.
header("Location: " . BASE_URL . "members/reset_password_results.php");
// End script.
exit();
}//End of VERIFY UPDATE
// Close prepared statement.
mysqli_stmt_close($stmt2);
// Close the connection.
mysqli_close($dbc);
If my ELSE branch fires, then does “exit()” close my Prepared Statement and DB Connection automatically?
I could do this, but am trying not to make my code overly verbose…
// Verify Update.
if (mysqli_stmt_affected_rows($stmt2)==1){
// Update Succeeded.
}else{
// Update Failed.
$_SESSION['resultsCode'] = 'PASSWORD_RESET_FAILED';
// Close prepared statement.
mysqli_stmt_close($stmt2);
// Close the connection.
mysqli_close($dbc);
// Redirect to Display Outcome.
header("Location: " . BASE_URL . "members/reset_password_results.php");
// End script.
exit();
}//End of VERIFY UPDATE
// Close prepared statement.
mysqli_stmt_close($stmt2);
// Close the connection.
mysqli_close($dbc);
Thanks,
Debbie
When PHP has finished running - e.g. when exit() is run, or when all code has been run - all resources are disposed of, as far as is relevant.
Exit is not a normal function - in fact, it isn’t a function, it’s a command. As soon as it has been run, nothing else will run.
Also mysqli_close and mysqli_stmt_close are unnecessary at the end of a script.
Jake_Arkinstall:
When PHP has finished running - e.g. when exit() is run, or when all code has been run - all resources are disposed of, as far as is relevant.
Exit is not a normal function - in fact, it isn’t a function, it’s a command. As soon as it has been run, nothing else will run.
Also mysqli_close and mysqli_stmt_close are unnecessary at the end of a script.
I’ve just been taught to run them to not leave anything hanging out there.
Debbie
The connection should be closed on script exit. In my experience that doesn’t always happen. The only way to be certain is to close it yourself.
Unless you happen to be using persistent connections (usually a bad idea) then no, you need not explicitly close database connections.
That is what I was taught…
Debbie
In this particular case you may been taught wrong. In PHP there is simply no reason to close connections prior to exiting.
Now you may have a long running script and it might make sense to close up just so someone else can use the connections. But for regular web scripts, nope.
I have had mysql’s top running on a server watching processes. They do not always close immediately with the script. I’m guessing that their closure is part of PHP garbage collection. Eventually the connection does get closed but it can linger for seconds or even minutes. On a server that isn’t being overworked this isn’t really important, but if the server hosts a lot of traffic it can quickly become one. So the best practice is to formally close the connection.