I know that closing prepared statements is a suggested thing to do.
But I have a PHP script like this:
$sql = "SELECT * FROM `mytable` WHERE ...";
$stmt = $dbh->stmt_init();
if($stmt->prepare($sql))
{
$stmt->bind_param("s", $user);
if($stmt->execute())
{
$result = $stmt->get_result();
$stmt->close();
} else
header("Location: .?error=unknown");
} else
header("Location: .?error=conn");
The statement gets closed if everything is ok, but when something fails during the execution it doesn't get closed.
Should I write
else {
$stmt->close();
header("Location: .?error=unknown");
}
and
else {
$stmt->close();
header("Location: .?error=conn");
}
or, since an error occurred, I shouldn't worry about closing the statement?
Or could I even write:
$sql = "SELECT * FROM `mytable` WHERE ...";
$stmt = $dbh->stmt_init();
if($stmt->prepare($sql))
{
$stmt->bind_param("s", $user);
if($stmt->execute())
{
$result = $stmt->get_result();
} else
header("Location: .?error=unknown");
} else
header("Location: .?error=conn");
/*some other code*/
$stmt->close; //close every statement at the very end of the script
or is it better to close the prepared statements right after I have finished using them to avoid any sort of bugs?