简体   繁体   English

连接到两个数据库

[英]Connecting to two databases

I have an application in which I want to authenticate a user from a first database & manage other activities from another database. 我有一个应用程序,我想在其中从第一个数据库对用户进行身份验证并从另一个数据库管理其他活动。 I have created two classes. 我创建了两个类。 An object of the classes is defined in a file: 类的对象在文件中定义:

$objdb1=new db1(),$objdb2=new db2();

But when I try to call $objdb1->fn() . 但是当我尝试调用$objdb1->fn() It searches from the $objdb2 & is showing table1 doesnot exists? 它从$objdb2搜索并显示table1不存在?

My first file database.php 我的第一个文件database.php

class database
{
private $hostname;
private $database;
private $username;
private $password;
private $dblinkid;

function __construct()
{
    if($_SERVER['SERVER_NAME'] == 'localhost')
    {
        $this->hostname = "localhost";
        $this->database = "aaaa";
        $this->username = "xxx";
        $this->password = "";
    }
    else
    {
        $this->hostname = "localhost";
        $this->database = "xxx";
        $this->username = "xxx";
        $this->password = "xxx";
    }
    $this->dblinkid = $this->connect();
}

       protected function connect()
{
    $linkid = mysql_connect($this->hostname, $this->username, $this->password) or die("Could not Connect ".mysql_errno($linkid));
    mysql_select_db($this->database, $linkid) or die("Could not select database ".mysql_errno($linkid)) ;
    return $linkid;
}

Similarly second file 同样是第二档

class database2
{
private $vhostname;
private $vdatabase;
private $vusername;
private $vpassword;
private $vdblinkid;

function __construct()
{
    if($_SERVER['SERVER_NAME'] == 'localhost')
    {
        $this->vhostname = "xxx";
        $this->vdatabase = "bbbb";
        $this->vusername = "xxx";
        $this->vpassword = "";
    }
    else
    {
        $this->vhostname = "localhost";
        $this->vdatabase = "xxxx";
        $this->vusername = "xxxx";
        $this->vpassword = "xxxx";
    }
    $this->vdblinkid = $this->vconnect();
}

        protected function vconnect()
{
    $vlinkid = mysql_connect($this->vhostname, $this->vusername, $this->vpassword) or die("Could not Connect ".mysql_errno($vlinkid));
    mysql_select_db($this->vdatabase, $vlinkid) or die("Could not select database ".mysql_errno($vlinkid)) ;
    return $vlinkid;
}

Third file 第三档

$objdb1 = new database();
$objdb2 = new database2();

Can you help me on this? 你能帮我吗?

Regards, 问候,

Pankaj 潘卡伊

Without knowing your classes, it is difficult to help. 不了解您的课程,很难提供帮助。 If you are using PDO , I can guarantee you that you can create multiple instances connected to different databases without any problem. 如果您使用的是PDO ,我可以保证您可以创建多个连接到不同数据库的实例,而不会出现任何问题。 If you are using the mysql_ family of functions you probably just forgot to set the link_identifier parameter (see here ). 如果您使用的是mysql_系列函数,您可能只是忘了设置link_identifier参数(请参阅此处 )。

However, having a class db1 and a class db2 sounds like a code smell to me. 但是,对我来说,拥有db1类和db2类听起来像是代码的味道。 You probably want to have two instances of the same class with different attributes. 您可能希望拥有具有不同属性的同一类的两个实例。

Each time you call mysql_connect() or the equivalent mysqli functions, if a connection already exists using those same credentials it gets reused - so anything you do to modify the state of the connection, including changing database, charsets, or other mysql session variables affects "both" connections. 每次调用mysql_connect()或等效的mysqli函数时,如果连接已经使用相同的凭据存在,则该连接将被重用-因此,您所做的任何修改连接状态的操作,包括更改数据库,字符集或其他mysql会话变量的影响“两个”连接。

Since you are using the mysql_connect() function you have the option to force a new connection each time but this is not supported on all the extensions (IIRC mysqli and PDO don't alow for this). 由于您使用的是mysql_connect()函数,因此您可以选择每次都强制建立一个新的连接,但是并非所有扩展都支持该连接(IIRC mysqli和PDO对此不支持)。

However IMHO this is the wrong way to solve the problem. 但是恕我直言,这是解决问题的错误方法。 It just becomes messy trying to keep track of what's connected where. 试图跟踪连接在哪里的东西变得很混乱。

The right way would be to specify the database in every query: 正确的方法是在每个查询中指定数据库:

 SELECT stuff FROM aaaa.first f, aaaa.second s
 WHERE f.something=s.something;

Most likely your class does not pass the appropriate connection resource to the database functions. 您的类很可能没有将适当的连接资源传递给数据库函数。 The second argument to eg mysql_query() is the connection resource. 例如mysql_query()的第二个参数是连接资源。 Simply store this resource in an instance variable on connection, and use it every time you do something with the database. 只需将此资源存储在连接时的实例变量中,并在每次对数据库执行操作时都使用它。

Your problem may be in checking if the SERVER_NAME is "localhost". 您的问题可能在于检查SERVER_NAME是否为“ localhost”。 Seems like you may be using the same connection strings in both classes. 好像您在两个类中都使用了相同的连接字符串。 What is $_SERVER['SERVER_NAME'] resolving to? $ _SERVER ['SERVER_NAME']解析为什么?

You're looking for the fourth parameter of mysql_connect() , which states that it shouldn't reuse existing connections: 您正在寻找mysql_connect()的第四个参数,该参数指出它不应该重用现有的连接:

$dbLink1 = mysql_connect($server, $user, $pass, true);
mysql_select_db($db1, $dbLink1);

$dbLink2 = mysql_connect($server, $user, $pass, true);
mysql_select_db($db2, $dbLink2);

mysql_query("SELECT * FROM table1", $dbLink1); // <-- Will work
mysql_query("SELECT * FROM table1", $dbLink2); // <-- Will fail, because table1 doesn't exists in $db2

将第四个参数作为true传递给mysql_connect可解决此问题。

 $linkid = mysql_connect($this->hostname, $this->username, $this->password,true) or die("Could not Connect ".mysql_errno($linkid));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM