8

Can someone tell me, when you for example update, insert, delete.. should you then close it like mysqli_stmt::close(); ? I checked PHP manual and don't understand what close() actually does.

Example:

$stmt = $dbh->prepare("SELECT `user_email` FROM `users` WHERE `user_email` = ? LIMIT 1");
$stmt->execute(array($email));
$stmt->close();

Next part of my question is, if as an example i had multiple update queries in a transaction after every execute() for each query i am executing within the transaction should i close them individually ? ... because it's a transaction not sure i need to use $stmt->close(); after each execute(); or just use one $stmt->close(); after all of them ?

4

2 回答 2

8

There is no close() method for PDO, instead to close a connection you just set the database variable to null - which will close the connection.

$stmt = null;

To answer your second question, you only need to close the connection once. After you've executed all the queries you need to do on the database.

于 2012-04-14T23:41:43.463 回答
2

It seems like you are using mysqli close method. The $stmt->close() methode simply just closes closes the the previous opened database connect (http://www.php.net/manual/en/mysqli.close.php).

EDIT: If it's PDO you are using, I simply do not understand, why you dont take advantage of the possibility of having named parametres instead of question marks. That is, why a lot of people choose PDO instead of mysqli - you might have a better comprehensive view of the queries/statements.

于 2012-04-14T23:37:14.327 回答