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

后端 未结 11 2127
青春惊慌失措
青春惊慌失措 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:48

    Try below code:

        $conn = mysql_connect("hostname","username","password");
        mysql_select_db("db1",$conn);
        mysql_select_db("db2",$conn);
    
        $query1 = "SELECT * FROM db1.table";
        $query2 = "SELECT * FROM db2.table";
    

    You can fetch data of above query from both database as below

    $rs = mysql_query($query1);
    while($row = mysql_fetch_assoc($rs)) {
        $data1[] = $row;
    }
    
    $rs = mysql_query($query2);
    while($row = mysql_fetch_assoc($rs)) {
        $data2[] = $row;
    }
    
    print_r($data1);
    print_r($data2);
    

提交回复
热议问题