Strahan Posted May 11, 2014 Share Posted May 11, 2014 Hi. I always wondered about this.. if I do: function getName($userid) { $pdo = new PDO("blah blah blah"); $sql = $pdo->query("blah blah"); $row = $sql->fetch(PDO::FETCH_ASSOC); return $row["UserName"]; $pdo = null; } Since the $pdo = null is after the return, does that mean the pdo object isn't getting closed? I've been doing functions like that this way since I wasn't sure: function getName($userid) { $pdo = new PDO("blah blah blah"); $retval = ""; $sql = $pdo->query("blah blah"); $row = $sql->fetch(PDO::FETCH_ASSOC); $retval = $row["UserName"]; $pdo = null; return $retval; } I was just wondering if it's necessary, or if the objects clean themselves up automatically anyway? Quote Link to comment https://forums.phpfreaks.com/topic/288391-properly-closing-a-pdo-connection-in-a-function/ Share on other sites More sharing options...
mac_gyver Posted May 11, 2014 Share Posted May 11, 2014 you should NOT open a database connection, run one query, then close the database connection. you should open one database connection in your main application code and pass it into any function/class that needs it, then close it after you are finished using it or let php close it automatically when your script ends. to answer your question though, the return statement ends execution of the function code. this causes all variables that are local to the function to be destroyed (unless you defined them as static.) this destroying of the local $pdo variable should cause the connection to be closed regardless of if your $pdo=null; statement gets executed (your second code example) or not (your first code example.) Quote Link to comment https://forums.phpfreaks.com/topic/288391-properly-closing-a-pdo-connection-in-a-function/#findComment-1479031 Share on other sites More sharing options...
Strahan Posted May 11, 2014 Author Share Posted May 11, 2014 Cool, thanks. So would this be better then? <?php stuff stuff stuff $pdo = new pdo blah blah blah stuff stuff stuff $pdo = null; function dosomething() { global $pdo; stuff } ?> Quote Link to comment https://forums.phpfreaks.com/topic/288391-properly-closing-a-pdo-connection-in-a-function/#findComment-1479035 Share on other sites More sharing options...
mac_gyver Posted May 11, 2014 Share Posted May 11, 2014 better, yes, but don't use the global keyword. pass the connection in as a runtime parameter - function dosomething($pdo) { stuff } Quote Link to comment https://forums.phpfreaks.com/topic/288391-properly-closing-a-pdo-connection-in-a-function/#findComment-1479036 Share on other sites More sharing options...
Solution Strahan Posted May 11, 2014 Author Solution Share Posted May 11, 2014 Ah, OK. Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/288391-properly-closing-a-pdo-connection-in-a-function/#findComment-1479042 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.