Reputation: 21
I am trying to reuse mysql connection. Hence I have a global variable in databasemanager.php class that returns a connection.
The problem is somehow on one particular page mysql is executing prior query as well.
Looks like there is some leftover query in connection object that gets executed if same connection is being reused . Is it possible ? how to solve this ..
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;
}
This how code lookslike
Upvotes: 0
Views: 1971
Reputation: 343
Have you looked into mysql_pconnect?
According to the documentation:
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()).
Upvotes: 1