Right now I open a connection (if one is not already opened) every time I think I will need one. The question is: when I do close it? At 开发者_开发知识库the end of each script that uses it? Is there a standard for this sort of thing?
If you're talking about PDO (your tag): Connections are automatically closed when your script terminates except you're using persistent connections.
It's not really necessary to close your database links yourself in normal web application unless you have long-running scripts (that don't require a continuous database connection).
I've always closed them as soon as I was finished with them. Why leave them open when you don't need them?
精彩评论