MySQL连接重用问题

标签 mysql

我正在尝试重用 mysql 连接。因此,我在databasemanager.php 类中有一个返回连接的全局变量。

问题出在某个特定页面上,mysql 也在执行先前的查询。

看起来连接对象中存在一些剩余查询,如果重用相同的连接,则会执行该查询。是否可以 ?如何解决这个问题..

 function getDBConnection(){
   global $conn;
   if (!empty($conn)){
// echo $conn ;
    return $conn;
   }
   $conn =  mysql_connect($GLOBALS['HOSTNAME'],$GLOBALS['DBUSER'],$GLOBALS['DBPASS']);

    if (!$conn) {
     die('Could not connect: ' . mysql_error());
    }

    mysql_select_db($GLOBALS['DBNAME'],$conn);
    return $conn;

}

这就是代码的样子

最佳答案

您查看过mysql_pconnect吗? ?

根据文档:

mysql_pconnect() acts very much like mysql_connect() with two major differences.

First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect()).

关于MySQL连接重用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1099762/

相关文章:

mysql - 尽管使用了连接,为什么这被认为是一个简单的选择?

mysql - 在存储过程中替换变量和游标的问题

php - 如何将表单数据插入mysql数据库

javascript - 使用 AngularJs 和 PHP 显示从 MYSQL 收集的数据到 html 页面

php - jQuery BlockUI 脚本上的重复区域无法工作

mysql - 三个不同表中的最大值

MySQL Select from table 返回全部,加上带有条件的一列和另一个查询

php - 检查mysql数据库中是否存在记录

java - 将数据从 MySql 转储到 MongoDB

mysql使用with语句在太多行上计算不同的数字