How to select more than one database tables using one query

2017-10-04 php mysql database mysqli

I want to displaying the value from more than one database mysql tables.

I have code like this :

$db1 = "SELECT * FROM db1 where no='1' ";
$db2 = "SELECT * FROM db2 where no='1' ";
$db3 = "SELECT * FROM db3 where no='1' ";  

and I want to use one query like this :

$sql = mysqli_query($connect, $db1);
while ($data = mysqli_fetch_array($sql)) 
   {
    bla bla bla...       
   }

is this possible if I used one query like that or there is any query to get example 3 database tables into one query? Thanks.

Answers

Nope you can not get data from multiple tables using one sql query without performing a join on them.

But you can use mysqli_multi_query to execute multiple queries.

Please check https://www.w3schools.com/php/func_mysqli_multi_query.asp

I am assuming you have three different databases in the same server. In this case, you can write the query in the below format Database.Schema.Table So in your case

$db1 = "SELECT * FROM Database.Schema.db1 where no='1' ";
$db2 = "SELECT * FROM Database.Schema.db2 where no='1' ";
$db3 = "SELECT * FROM Database.Schema.db3 where no='1' ";

Hope this is what you are looking for!

Related