<?php $servername = "localhost"; $username = "username"; $password = "password"; try { $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?> 

1) $ conn-> close ();

2) mysqli_close ($ conn);

2) $ conn = null;

  • First option. - etki
  • That's for sure ? I'm at a loss - user33274
  • This is the first time I ask myself such a question (I have never worked with PDO directly), but yes, the responsibility to close the resources behind me lies with the developer. mysqli_close has nothing to do here. - etki
  • another question, why do you need to close them at all? if you write the server (God forbid), they are reused, if you write the web, they close with the end of the script. if you have a script that connects to a MILLION DB, then you have an interesting situation, tell - strangeqargo

1 answer 1

Strange you have the answers. The PDO does not have a close method, in principle, what’s the mysqli_close not clear.

In the documentation, the answer is given and quite clearly:

If you want to make it so, you can assign it. If your script ends.

Remove all references to the PDO object and it will close the connection. You can assign NULL , you can do unset .