Re: Connecting to two MySQL databases

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I am working on an application that requires me to connect to two MySQL databases both hosted on the same server.

I have an existing set of class files I created to handle the db connection to the main database. To connect to the second database, I duplicated my db class files and renamed them, and also renamed the functions etc. but what is happening, is that the calls made to the second database connection seem to get made against the first database. So that I get errors like: dbname.tablename was not found (or something like that).

What is the best practice for this? I need to have two open db connections for just two little things in my application.

In my opinion, rather than making two different classes that do the same thing, I would make one class and then create two instances of that class in your script. For this to work, you'll need to make the db host, username, password, and database name be values that are passed to the constructor when the object is created. Assuming you have that, then your code would be something like...

$dbc1 = new dbconn('localhost', 'test', 'test', 'mydb');
$dbc2 = new dbconn('localhost', 'test', 'test', 'otherdb');

Then invocations of $dbc1->retrieveData() would to go the one database whereas $dbc2->retrieveData() would go to the other. I could be missing something, of course, but that's my first thought.

Also, just to be safe, you could use the new link argument in mysql_connect() (the 4th argument) to insure a new, separate connection.

Hope that helps,
Larry

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux