3

i use this code, for example:

$result = $link->prepare("SELECT * FROM animals WHERE id = '$id'");
$result->execute();
$row = $result->fetch(PDO::FETCH_ASSOC);
$result = null;
$link = null;

It's ok use "$result = null;" to close this connection?

1 Answers1

2

It's ok use "$result = null;" to close this connection?

It would not close the database connection. However it closes the PDOStatement $result , which is probably what you were concerned about.

If you want to close the connection with all it's associated resources, you do fine with

$link = null;

Which you have, too. So I don't see any problems at all here. I must even admit, I wonder a bit what made you asking.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • Thanks for the reply, then is fine close each execute(); and also the connection ($link)?, basically the two at the same time ($link = null; only once time to the end). – Francisco Torres Dec 29 '12 at 23:08
  • Are you running into resource problems? If not, I would not care much actually. It's also okay to just write `$link = null`. – hakre Dec 29 '12 at 23:10
  • Yes, I have problems with server resources, i take your advice;) – Francisco Torres Dec 29 '12 at 23:13
  • Which kind of problems? Too many database connections or memory? – hakre Dec 29 '12 at 23:17
  • Too many simultaneous connections, exactly this error "Prepared statement needs to be re-prepared" when i use PDO for the connection, using MySQL i get the error "Warning, too many simultaneous connections." :s i solved it already, but i do not want to happen again – Francisco Torres Dec 30 '12 at 00:57