PHP/MySQL:为每个查询创建新连接?
我有一个包含一些 mysql 查询的网页,我必须在每次查询后启动新的数据库连接并关闭数据库,或者更有效地在代码顶部启动一个数据库连接并在脚本底部关闭?谢谢!
I have a webpage with some mysql querys, I must start new db connection and db close after every query or its more efficiently start an only one db connection at the top of the code and close at the bottom of the script? Thanks!
推荐答案
从不为每个查询创建新连接;这非常浪费,并且会使您的 MySQL 服务器负担过重.
Never create a new connection for every query; it's very wasteful and will overburden your MySQL server.
在脚本顶部创建一个连接就足够了 - 所有后续查询都将使用该连接,直到页面结束.您甚至可以在将 include()
编入脚本的其他脚本中使用该连接,并在其中调用 mysql_connect()
.
Creating one connection at the top of your script is enough - all subsequent queries will use that connection until the end of the page. You can even use that connection in other scripts that are include()
ed into the script with your mysql_connect()
call in it.
相关文章