简体   繁体   中英

mysqli PHP search and filter by two databases/tables in MySQL

I am trying to search and filter by two different databases.

DATABASE1.table1  |   DATABASE2.table1
id | col2         |   id | ref_col2 | ref_col3
1  | 2            |   1  | thing    | thing
2  | 500          |   2  | other    | other

I know you can use the following if its on the same database:

$search = explode(" ", mysqli_real_escape_string($connection, $_POST["search_string"]));
$search_string = " AND (";
for($i=0;$i<count($search);$i++){
    $search_string .= "(t2.ref_col2 LIKE '%".$search[$i]."%' OR t2.ref_col3 LIKE '%".$search[$i]."%') AND ";}
$search_string = substr($search_string,0,strlen($search_string)-4);
$search_string .= ")";  


$query = mysqli_query($connection, "SELECT t1.*, t2.* AS row_count FROM table1 AS t1 LEFT JOIN  table2 AS t2 ON t1.id = t2.id WHERE t1.id>0".$search_string)or die();

My question is: What if they are on two different databases? is there a specific way to select one database.table results by searching another database.table's list the above?

if they are on the same server then

database1.table1 AS t1 LEFT JOIN  database2.table2 AS t2 

should be fine

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