我正在尝试重用 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/