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

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

    if you are using mysqli and have two db_connection file. like first one is

    define('HOST','localhost');
    define('USER','user');
    define('PASS','passs');
    define('**DB1**','database_name1');
    
    $connMitra = new mysqli(HOST, USER, PASS, **DB1**);
    

    second one is

        define('HOST','localhost');
        define('USER','user');
        define('PASS','passs');
        define(**'DB2**','database_name1');
    
        $connMitra = new mysqli(HOST, USER, PASS, **DB2**);
    

    SO just change the name of parameter pass in mysqli like DB1 and DB2. if you pass same parameter in mysqli suppose DB1 in both file then second database will no connect any more. So remember when you use two or more connection pass different parameter name in mysqli function

    0 讨论(0)
  • 2020-11-22 05:47
    <?php
        // Sapan Mohanty
        // Skype:sapan.mohannty
        //***********************************
        $oldData = mysql_connect('localhost', 'DBUSER', 'DBPASS');
        echo mysql_error();
        $NewData = mysql_connect('localhost', 'DBUSER', 'DBPASS');
        echo mysql_error();
        mysql_select_db('OLDDBNAME', $oldData );
        mysql_select_db('NEWDBNAME', $NewData );
        $getAllTablesName    = "SELECT table_name FROM information_schema.tables WHERE table_type = 'base table'";
        $getAllTablesNameExe = mysql_query($getAllTablesName);
        //echo mysql_error();
        while ($dataTableName = mysql_fetch_object($getAllTablesNameExe)) {
    
            $oldDataCount       = mysql_query('select count(*) as noOfRecord from ' . $dataTableName->table_name, $oldData);
            $oldDataCountResult = mysql_fetch_object($oldDataCount);
    
    
            $newDataCount       = mysql_query('select count(*) as noOfRecord from ' . $dataTableName->table_name, $NewData);
            $newDataCountResult = mysql_fetch_object($newDataCount);
    
            if ( $oldDataCountResult->noOfRecord != $newDataCountResult->noOfRecord ) {
                echo "<br/><b>" . $dataTableName->table_name . "</b>";
                echo " | Old: " . $oldDataCountResult->noOfRecord;
                echo " | New: " . $newDataCountResult->noOfRecord;
    
                if ($oldDataCountResult->noOfRecord < $newDataCountResult->noOfRecord) {
                    echo " | <font color='green'>*</font>";
    
                } else {
                    echo " | <font color='red'>*</font>";
                }
    
                echo "<br/>----------------------------------------";
    
            }     
    
        }
        ?>
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 05:51

    I just made my life simple:

    CREATE VIEW another_table AS SELECT * FROM another_database.another_table;
    

    hope it is helpful... cheers...

    0 讨论(0)
提交回复
热议问题