Jump to content

connecting to two mysql databases simultaneously


klb

Recommended Posts

is it possible to have two open connections to two different mysql dbs at the same time?  when i tried it, only the one on the bottom of the list was active.

 

my config file looks like this:

 

//---------------------------------------------//

 

$dbname = 'xxx';      # Database Name

$dbuser = 'xxx';      # Database Username

$dbpass = 'xxx';      # Database Password

$dbhost = 'xxx';        # Database Host

 

$conn2 = mysql_connect($dbhost,$dbuser,$dbpass) or die ("Could not connect to $dbname: ".mysql_error());

mysql_select_db($dbname) or die ("Could not access the database: ".mysql_error());

 

$dbname5 = 'yyy';      # Database Name

$dbuser5 = 'yyy';      # Database Username

$dbpass5 = 'yyy';      # Database Password

$dbhost5 = 'yyy';        # Database Host

 

$conn = mysql_connect($dbhost5,$dbuser5,$dbpass5) or die ("Could not connect to $dbname5: ".mysql_error());

mysql_select_db($dbname5) or die ("Could not access the database: ".mysql_error());

 

//--------------------------------------//

 

 

so i want to be able to do

 

mysql_query($query,$conn2) when i need to access xxx db, but it doesn't seem to work that way.  am i doing something incorrectly? any help would be greatly appreciated.

  • 3 years later...

to resolve this

 

akitchin had suggested athis link

 

http://ca3.php.net/manual/en/function.mysql-connect.php

 

Just to under stand more  can I we give

 

$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');

 

A connection to 1st database for example.com

 

$link = mysql_connect('example2.com:3307', 'mysql_user', 'mysql_password');

 

A connection to 2nd database for example2.com

 

Is this OK ?

 

Also want to know how the port number is given  who allocates this ?

 

Does this mean these websites are from same name servers ?

 

Actual code :suggested by akitchin

 

 

Link : http://ca3.php.net/manual/en/function.mysql-connect.php

 

Code

<?php

// we connect to example.com and port 3307

$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');

if (!$link) {

    die('Could not connect: ' . mysql_error());

}

echo 'Connected successfully';

mysql_close($link);

 

// we connect to localhost at port 3307

$link = mysql_connect('example2.com:3307', 'mysql_user', 'mysql_password');

if (!$link) {

    die('Could not connect: ' . mysql_error());

}

echo 'Connected successfully';

mysql_close($link);

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.