0

is it possible to connect to multiple database? E,g. "database1, database2".

if is not possible, anyone can show me how to connect, multiple database.

$db_host        = 'localhost';
$db_user        = 'username';
$db_pass        = 'password';
$db_database        = 'username_database1, username_database2'; 

/* End config */


$link = @mysql_connect($db_host,$db_user,$db_pass) or die('Unable to establish a DB connection');

mysql_set_charset('utf8');
mysql_select_db($db_database,$link);

thank you.

Michal Čihař
  • 9,799
  • 6
  • 49
  • 87
nasir
  • 23
  • 5
  • 2
    yes it is possible. option one the user has access to all dbs, option 2 create a connection resource to each –  Aug 23 '13 at 01:16

3 Answers3

2

Yes this question has been answered before, but you should try to use mysqli instead.

The PHP mysql extension is officially deprecated in 5.5

The code below is for those planning to use mysqli (procedural-form) to connect to multiple database. You can check to see that it does indeed work with more than one database.

$db1 = @mysqli_connect('hostname', 'username', 'password', 'DB1');
$db2 = @mysqli_connect('hostname', 'username', 'password', 'DB2');
$obj1 = array();
$obj2 = array();
if  ($res = mysqli_query($db1, 'SHOW TABLES')) {
    while ($obj = mysqli_fetch_object ( $res )) $obj1[] = $obj;
    mysqli_free_result ( $res );
}
if  ($res = mysqli_query($db2, 'SHOW TABLES')) {
    while ($obj = mysqli_fetch_object ( $res )) $obj2[] = $obj;
    mysqli_free_result ( $res );
}
var_dump($obj1);
echo '<hr/>';
var_dump($obj2);
echo '<hr/>';
@mysqli_close($db1);
@mysqli_close($db2);
die('');
Aaron Gong
  • 977
  • 7
  • 18
1

Yes, it is possible. However, this question has already been asked more than once. Please follow the rules! Here is the previous link.

Community
  • 1
  • 1
tjons
  • 4,749
  • 5
  • 28
  • 36
  • 2
    lol, would that be the rules regarding the difference between a comment and an answer? –  Aug 23 '13 at 01:22
  • 1
    I am sorry. I don't have enough reputation to use a comment. I certainly will use one once I get there! – tjons Aug 23 '13 at 01:30
1

It's doable, but I would really discourage you from running a single transaction across two datasources. That's called an XA transaction and in my experience only leads to headaches. There are all sorts of issues with one datasource timing out and causing problems in database drivers.

If you just want to connect and retrieve data from two datasources, just use two connections. Or if you're using mysql(and both are on the same server as in your example) you can just use a different database prefix and get to a table in a different schema.

select *
from schema1.table1 t1
inner join schema2.table2 t2 on t1.id = t2.id

For saving to two different databases I'd consider if your data model is correct. Two tables that should be in the same transaction should probably be in the same database. If that's not possible I prefer to save to one temporary table instead of both real ones at once. Then a timer based task can come along and move the data across.

Jeff Scott
  • 326
  • 2
  • 6