Close and/or kill mysqli connection at the end of the php script?

别说谁变了你拦得住时间么 提交于 2019-12-11 23:39:20

问题


Do I need to explicitly kill and/or close a mysqli connection at the end of a script. At the moment, I do neither, and it works, but I don't have much load.

I've heard that there is connection pooling, and mysqli connections are re-used... do I need to close the connection then at all?


回答1:


You should always close your connections at the end of the main script. If you uses some php5 and object to handle your DB just think about using __destruct() to automatically close it

http://php.net/manual/en/language.oop5.decon.php




回答2:


No it is not necessary to close a mysql connection at the end of a script. PHP does it automatically.




回答3:


it is not necessary but generally that's good practice. I personally, when wish to boost error-checking mechanism, you $con->close() for almost each function. e.g:

if($stmt = $db->prepare($statment))
{
    if($db->bind_param($str, $param1, $param2))
    {
        // the rest of the code and error checking
    }
    else
    }
       $db->close();
    }
}
else
{ 
 $db->close();
}

This makes OOP necessary. Since you can simply write function doing this automatically with many other security checks added. codeIgniter as a framework has nice DB connectors whith superb security at a basic general level.



来源:https://stackoverflow.com/questions/5232458/close-and-or-kill-mysqli-connection-at-the-end-of-the-php-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!