Connecting to TWO DB's

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
dstefani
Forum Contributor
Posts: 140
Joined: Sat Jan 11, 2003 9:34 am
Location: Meridian Idaho, USA

Connecting to TWO DB's

Post by dstefani »

I have two MySQL Databases I need to connect to.
I'm running into some problems.

Trying to use an included file with these contents:

Code: Select all

$dblink = mysql_connect('localhost', 'user', 'password');
$dblink2 = mysql_connect('localhost', 'user', 'password');

mysql_select_db('db1', $dblink);
mysql_select_db('db2', $dblink2);
You would think that this would work, but it will only use one connection or the other, not both.

I've even tried to put them into seperate files and it still won't work, any suggestions?

Thanks,

- D
User avatar
dstefani
Forum Contributor
Posts: 140
Joined: Sat Jan 11, 2003 9:34 am
Location: Meridian Idaho, USA

Post by dstefani »

OK, again I'm answering my own question:
Perhaps this will help the next poor slob! :wink:

I made the second mysql_connect use a different user, so I did not have identical link identifiers.

That calls for a mild "DUH!". But at least I found it.

Have fun!

- D
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

just out of curiosity, why are you using two dbs?

if you can have all the tables in one db it might be easier to deal with. it might be worth looking into.
User avatar
dstefani
Forum Contributor
Posts: 140
Joined: Sat Jan 11, 2003 9:34 am
Location: Meridian Idaho, USA

Post by dstefani »

The first DB is a a huge collection of data as well as being used for a very busy ecommerce site. The new application I'm building uses 3 tables from the larger Db (products) and the 2nd DB is for a 2nd completely seperate application that is in it's first version, with the potential for allot of growth.

Since the data in most ways is VERY unrelated, I thought for clarity and ease of use it would be best to keep them seperate. Mostly for future potential growth sake.

It's a first for me, I went for it. :)

- D
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Are you using two MySQL servers or just two MySQL databases? You shouldn't need two connections to the same server if you need to access two databases on it. Simply use mysql_select_db() to move between the databases.

Mac
User avatar
dstefani
Forum Contributor
Posts: 140
Joined: Sat Jan 11, 2003 9:34 am
Location: Meridian Idaho, USA

Post by dstefani »

In my original post I showed how I was trying to use my included file to do that, to use an include wouldn't I have to have two mysql_connect()'s?
And then use the different connect variables in my mysql_db_select?

PHP wanted to use just on connection if the params for the connection were the same. When I changed the params on one of the connections, then it worked. I got the idea to try this from the manual - mysql_connect():
If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters.
It seemed to me that if I didn't use different params for my second connect it would never work. To answer you question: same server two DB's.

What do you think?

Thanks
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

This could aid you. The usage of it may vary, and others have other ideas. I'm just providing some ideas you can play around with.

Code: Select all

File: db.php
<?php
    // database manager
    function db($database, $query) {
        // main connect
        $dblink = mysql_connect('localhost', 'user', 'password'); 
        // select the preffered database using the provided variable
        mysql_select_db($database, $dblink);
        // fetch the results
        $result = mysql_query($query, $dblink);
        // close the link (if any)
        mysql_close($dblink);
        // return the results
        return $result;
    }
?>

File: index.php
<?php
 // require (or include) the database file and it's function
 require('db.php');
 // try it out, calling the function with particular db and query.
 $testdb1 = db('db1',"select * from foo where bar = 'kittythrow'");
 $testdb2 = db('db2',"select * from bar where foo = 'cowthrow'");
 // and so on
?>
User avatar
dstefani
Forum Contributor
Posts: 140
Joined: Sat Jan 11, 2003 9:34 am
Location: Meridian Idaho, USA

Post by dstefani »

That's cool - thanks

- D
Post Reply