Connect to Multiple Databases using MySQLi

后端 未结 3 1677
花落未央
花落未央 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

    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.

提交回复
热议问题