doulai2025 2014-05-13 10:02
浏览 50

无法使用会话处理程序中定义的连接代码查询MySql表

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?

  • 写回答

1条回答 默认 最新

  • dqwh2717 2014-05-13 16:35
    关注

    Though I haven't had time to merge these two connection queries into one, mathematical logic says that

    if ($db = mysql_pconnect($hostname_AB, $username_AB, $password_AB))
    {
    return mysql_select_db('sessionhandler', $db);
    }
    

    the above block of equation confines the database connection to a single table sessionhandler and primarily that's why query to other tables using this connection code is invalid.

    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端