doujichan1399
2013-04-11 08:38
浏览 19
已采纳

从另一个数据库获取数据

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!

图片转代码服务由CSDN问答提供 功能建议

我目前在一个数据库上,用于例如:user 。 我正在从该数据库中检索数据。 但我需要从另一个数据库获取数据,如:

  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); 
} 
   
 
 

但显示错误:未定义的索引名称。 我认为这是因为数据库的更改和查询。

我无法想出另一种方法。 你能说另一种实现方法吗?

谢谢!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duannao8450 2013-04-11 08:47
    已采纳

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doufocheng6233 2013-04-11 08:52

    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 ...

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题