Connect to Multiple Databases using MySQLi

后端 未结 3 1676
花落未央
花落未央 2021-01-20 21:44

I need to connect to two databases using PHP and use the results from the first query to get the rest of the data I need out of a second database.

So for the second c

相关标签:
3条回答
  • 2021-01-20 22:06

    You would need to make 2 different connections

    <?php
        $mysqliDB1 = new mysqli('localhost', 'DB1UserId', 'pwd', 'db1');
    
        $mysqliDB2 = new mysqli('localhost', 'DB2UserId', 'pwd', 'db2');
    

    Now when you use the $mysqliDB1->.... you are talking to the DB1 database and when you use the $mysqliDB2->.... you are talking to the DB2 database

    So

    $client = $mysqliDB1->query("SELECT client FROM appointments WHERE ID = $results")
             ->fetch_object();
    
    $locn = $mysqliDB2->query("SELECT state,zipcode 
                               FROM location 
                               WHERE ClientID = {$client->FirstName}")
                      ->fetch_object();
    echo $locn->state;
    echo $locn->zipcode;
    

    I am guessing the table name and so on, but I am not clarevoyant so you will have to fill that in for yourself.

    0 讨论(0)
  • 2021-01-20 22:08

    If you want to perform queries in two databases at the same time you need to have two separate mysqli objects. To open the connection you can use the following code:

    // Don't forget to enable error reporting!
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    
    $db1 = new mysqli('localhost', 'user', 'pass', 'dbName');
    $db1->set_charset('utf8mb4'); // always set the charset
    
    $db2 = new mysqli('localhost', 'user', 'pass', 'dbName2');
    $db2->set_charset('utf8mb4'); // always set the charset
    

    Then you can perform your two statements in each database separately.

    // get id value
    $id = intval($_GET['cd']);
    
    // Get client name from DB1
    $stmt = $db1->prepare('SELECT client FROM appointments WHERE ID = ?');
    $stmt->bind_param('s', $id);
    $stmt->execute();
    $client = $stmt->get_result()->fetch_object();
    
    // Get state and zipcode from DB2
    $stmt = $db2->prepare('SELECT state,zipcode FROM location WHERE ClientName  = ?');
    $stmt->bind_param('s', $client->client);
    $stmt->execute();
    $location = $stmt->get_result()->fetch_object();
    
    echo $location->state;
    echo $location->zipcode;
    
    0 讨论(0)
  • 2021-01-20 22:17

    This isn't tested, but I think it would go something like this.

    <?php
    
    $dbc1 = new MySQLi()or die('error connecting to database');
    $dbc2 = new MySQLi()or die('error connecting to database');
    
    
    
    //build query 1
    $query1 = "SELECT * FROM Table";
    
    $result1 = $dbc1->query($query) or die("Error in query");
    $thing1 = '';
    // check result
    if($result1->num_rows){
        //fetch result as object
        $row = $result1->fetch_object();
    
        //set attributes
        $thing1 = $row->Name;
    }   
    
    
    //build query 2
    $query2 = "SELECT * FROM AnotherTable WHERE Id = '$thing1'";
    
    $result2 = $dbc2->query($query) or die("Error in query");
    $thing2 = '';
    // check result
    if($result2->num_rows){
        //fetch result as object
        $row = $result2->fetch_object();
    
        //set attributes
        $thing2 = $row->Name;
    }
    
    ?>
    
    0 讨论(0)
提交回复
热议问题