简体   繁体   中英

In php the mysqli how to compare two table data in different database

How to code compare data after done mysqli connect.

Database name garden_fruit1, password 123456, database username garden_fruit1.

$link1 = mysqli_connect("localhost", "garden_fruit1", "123456", "garden_fruit1"); 

// Check for connection 
if($link1 == true) { 
    echo "garden_fruit1 Connected Successfully"; 
} 
else { 
    die("ERROR: Could not connect " . mysqli_connect_error()); 
} 

Database name garden_fruit2, password 123456, database username garden_fruit2.

$link2 = mysqli_connect("localhost", "garden_fruit2", "123456", "garden_fruit2"); 

// Check for connection 
if($link2 == true) { 
    echo "garden_fruit2 Connected Successfully"; 
} 
else { 
    die("ERROR: Could not connect " . mysqli_connect_error()); 
} 

Example database table.

fruit1 //from garden_fruit1 database 
===========================
|id_fruit_1   fruit    price
|1            apple    3.00
|2            grape    5.00


fruit2 //from garden_fruit2 database 
===========================
|id_fruit_2   fruit    price
|1            apple    3.00
|2            grape    5.00

Get connect to database

$data1 = mysqli_query($link1,"SELECT id_fruit_1 FROM fruit1"); //database garden_fruit1
$data2 = mysqli_query($link2, "SELECT id_fruit_2 FROM fruit2"); //database garden_fruit2

So, I want to ask how to do the compare data from difference database?

You can use database prefix added to your tables:

SELECT id_fruit_1 FROM garden_fruit1.fruit1
SELECT id_fruit_2 FROM garden_fruit2.fruit2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM