I use the following connection code in open
function of a session handler file named sessionhandler.php
for establishing connection between session handler and MySql database named AB
, for example.
<---sessionhandler.php--->
function _open()
{
global $db;
$hostname_AB = "localhost";
$database_AB = "database";
$username_AB = "user";
$password_AB = "pass";
$AB = mysql_pconnect($hostname_AB, $username_AB, $password_AB) or trigger_error(mysql_error(),E_USER_ERROR);
$db = mysql_select_db($database_AB, $AB) or die("Couldn't select database");
if ($db = mysql_pconnect($hostname_AB, $username_AB, $password_AB))
{
return mysql_select_db('sessionhandler', $db);
}
return FALSE;
}
function _close()
{
global $db;
return mysql_close($db);
}
The problem is that, I can't make query to any other table for example my_table1
, my_table2
, etc.
of AB
database using the connection code quoted in the first block above. The first code block only establishes connection between the session handler file sessionhandrer.php
& database table sessionhandler
only.
In order to make query to other tables of the database AB
I had to use seperate connection file that contains the following code:
$hostname_AB = "localhost";
$database_AB = "database";
$username_AB = "user";
$password_AB = "pass";
$AB = mysql_pconnect($hostname_AB, $username_AB, $password_AB) or
trigger_error(mysql_error(),E_USER_ERROR);
Example of a query using above connection code that ignores the connection code of sessionhandler.php
:
mysql_select_db($database_AB, $AB);
$query_test = "SELECT users.id, users.name FROM users WHERE users.id = >= 1";
$test = mysql_query($query_test, $AB) or die (mysql_error());
$row_test = mysql_fetch_assoc($test);
$totalRows_test = mysql_num_rows($test);
Absence of the above connection file causes typical MySql connection error in case there are any queries there from other tables of AB
database.
How shall I combine these two connection files for making queries to all tables of database AB
?