我有一个包含以下代码的数据库连接文件:
$my_db_link = mysql_connect('my_host', 'my_username', 'my_password');
if (!$my_db_link) {
die('Could not connect: ' . mysql_error());
}
$my_db_name = 'my_database';
mysql_select_db($my_db_name);
如果我使用 include_once() 而不是 include(),或者使用 require_once() 而不是 require(),会有不同吗?我们最近遇到了“连接过多”错误(“PHP 警告:mysql_connect() [function.mysql-connect]:连接过多”),我想知道使用 include() 是否创建了不必要的连接数。
include_once('my_db_connect.php');
代替
include('my_db_connect.php');
最佳答案
The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.
require_once 的手册是 here
The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.
使用持久连接可能需要对 Apache 和 MySQL 配置进行一些调整,以确保不超过 MySQL 允许的连接数。
因此最好使用 mysql_pconnect()
来防止连接过载。查看说明书here
关于php - 防止过多的数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6230691/