How do you connect to multiple MySQL databases on a single webpage?

后端 未结 11 2134
青春惊慌失措
青春惊慌失措 2020-11-22 05:06

I have information spread out across a few databases and want to put all the information onto one webpage using PHP. I was wondering how I can connect to multiple databases

11条回答
  •  别跟我提以往
    2020-11-22 05:50

    $dbh1 = mysql_connect($hostname, $username, $password);  
    $dbh2 = mysql_connect($hostname, $username, $password, true); 
    
    mysql_select_db('database1', $dbh1); 
    mysql_select_db('database2',$dbh2); 
    
    mysql_query('select * from tablename', $dbh1);
    mysql_query('select * from tablename', $dbh2);
    

    This is the most obvious solution that I use but just remember, if the username / password for both the database is exactly same in the same host, this solution will always be using the first connection. So don't be confused that this is not working in such case. What you need to do is, create 2 different users for the 2 databases and it will work.

提交回复
热议问题