php - 防止过多的数据库连接

标签 php

我有一个包含以下代码的数据库连接文件:

$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/

相关文章:

php - 如何使用 jQuery 和 mySQL 转义和取消转义字符串

php - 如何在 PHP Symfony 中设置和销毁 session

php - 什么是长轮询、Websocket、服务器发送事件 (SSE) 和 Comet?

php - undefined offset : 0 in edit function

php - mysql, php - 字符编码问题`

Java 转换 func_get_args PHP、varargs

php - 多个文件存在检查?更好的方法?

php 是否可以确定常量是否是用户定义的?

php - PDO 准备错误

php - laravel 5.1 中的社交登录?