从另一个数据库获取数据

I am currently on one database, for eg: user. I am retrieving data from that database. But i need to get data from another database like:

mysqli_select_db($link, "first");
$q1="select * from user";
$s1=mysqli_query($link,$q);
while($row=mysqli_fetch_assoc($s1))
{
 mysqli_select_db($link, "second");
 $q2="select * from ".$row['name'];
 $s2=mysqli_query($link,$q2);
 echo mysql_num_rows($s2);
}

But showing error: undefined index name. I think it is because of the database change and the queries.

I could not figure out another way to do this. Can you please say another way to implement this?

Thanks!

You define your query in $q1 but then call mysqli_query() with $q which is a different variable and I'm guessing you have a previous query stored in that, which would explain the undefined index notice.

Another problem is you're using the same $link variable for two different databases. I suggest storing the first in $link_first and the second in $link_second to keep them separated.

you should change

$s1=mysqli_query($link,$q);

to

$s1=mysqli_query($link,$q1);

because your query is stored in $q1

and you can take

mysqli_select_db($link, "second");

out of while loop.. why are you selecting the database with every iteartion.. ? Once selected it will be there until you chane it again ...