Do I need to use mysql_close(connection)?

后端 未结 2 992
暖寄归人
暖寄归人 2020-12-22 08:55

I have the following code in db.php to connect to my DB.



        
相关标签:
2条回答
  • 2020-12-22 09:25

    If you don't close your connections, they will stay open and take up precious resources on the server. I guess there's there the security point too, you don't want to risk someone getting a hold of the connection.
    I prefer to put my database in a class and use __construct to create the connection, and __destruct to close the connection. If your unfamiliar with classes. The __construct and __destruct gets called automatically when you create and destroy a class.

    Edit:
    There was originally meant to be an example. But I have a basic but working mysql class here https://stackoverflow.com/a/9651249/1246494.

    It shows the usage of mysql_close and how I was trying to relate it to the class destructor. The point was, any network connection should be closed, whether your database is on a remote server, or localhost.

    0 讨论(0)
  • 2020-12-22 09:46

    No, this won't help you if you close it at the end of the script. mysql_close() is just useful in case you want to free up resources before you end your script, because your connection is closed as soon as execution of the script ends

    0 讨论(0)
提交回复
热议问题