Jump to content

Recommended Posts

This is what I need to do (connect to two databases at same time)

 

$con1 = mysql_connect("server", "user", "pass");
if (!$con1)
{
echo "Could not connect to db 1: ". mysql_error();
exit;
}

//connect to db 2
$con2 = mysql_connect("server", "user", "pass");
if (!$con2)
{
echo "Could not connect to db 2: ". mysql_error();
exit;
}


//select db 1
mysql_select_db("dbase-name", $con1);

 

The problem is the above will not work - it doesn't select the first connection again - as though it has been lost.

 

However if I check if($con1) it DOES exist.

 

If I tried to perform queries on connection 2 - this would work.

Are the databases on the same server. You dont have to open 2 connections if so, you can prepend the table name with the database name:

mysql_query("SELECT * FROM db1.tablename ",$conn);
mysql_query("SELECT * FROM db2.tablename ",$conn);

Just dont use mysql_select_db()

by very its nature mysql_connect uses only one connection at a time to save resources  , try to close the first connection b4 using another or use

 

$conn1 = new mysqli($host,$username,$password,$databasename);

 

$result = $conn->query('select.....');

 

$conn2 = new mysqli($host2,$username2,$password2,$databasename2);

$result2 = $conn2->query('select.....');

 

for this make sure mysqli extension is loaded ....

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.