做个搜索数据库名 test1 test2要怎么做,才能搜索出这两个数据库,相同表相同字段里的内容,一起显示

解决方案 »

  1.   

    [code=PH]
    <?php
    $conna = mysql_connect('localhost','root','root');
    $connb = mysql_connect('localhost','root','root');
    mysql_select_db('mytest',$conna); //mytest
    mysql_select_db('mmtest',$connb); //mmtest
    mysql_set_charset('gbk',$conna); //设置字符集
    mysql_set_charset('gbk',$connb); //设置字符集
    $sql = "select * from mmtest.info union all (select * from mmtest.info)"; 
    //union all 是把两个表的数据都显示出来;union 把两个表的数据显示出来,并去掉重复的。
    $query = mysql_query($sql);
    while($re = mysql_fetch_array($query)){
      echo $re['id']."--".$re['url']."<br />\n";
    }
    ?>
    [/code]输出:
    2--http://bbs.wanku.com/
    660--http://bbs.uc.cn/
    1267--http://bbs.yys5.com/index.php
    1379--http://67.220.92.027/forum/index.php
    2041--http://www.eaf1.cn/bbs/index.php
    2842--http://www.tsmm.com/bbs/
    2852--http://my.chinacars.com/bbs/
    2915--http://bbs.changjiang.cc/
    2954--http://www.panamachinese.com/bbs/
    3544--http://www.gochina.cn/bbs/
    3902--http://bbs2.ispeak.cn/
    4093--http://67.220.92.017/forum/
    4303--http://www.kokwind.com/bbs/
    4374--http://www.netyourlife.net/forum/
    4495--http://www.379.cc/
    4677--http://zenmela-net.osteching.com/bbs/
    5404--http://bbs.hcbbs.com/viewthread.php?tid=41408
    6164--http://www.bbsda.info/viewthread.php?tid=55184
    8995--http://bbs.ipark.cn/archiver/tid-16059.html
    9626--xxx
    2--http://bbs.wanku.com/
    660--http://bbs.uc.cn/
    1267--http://bbs.yys5.com/index.php
    1379--http://67.220.92.027/forum/index.php
    2041--http://www.eaf1.cn/bbs/index.php
    2842--http://www.tsmm.com/bbs/
    2852--http://my.chinacars.com/bbs/
    2915--http://bbs.changjiang.cc/
    2954--http://www.panamachinese.com/bbs/
    3544--http://www.gochina.cn/bbs/
    3902--http://bbs2.ispeak.cn/
    4093--http://67.220.92.017/forum/
    4303--http://www.kokwind.com/bbs/
    4374--http://www.netyourlife.net/forum/
    4495--http://www.379.cc/
    4677--http://zenmela-net.osteching.com/bbs/
    5404--http://bbs.hcbbs.com/viewthread.php?tid=41408
    6164--http://www.bbsda.info/viewthread.php?tid=55184
    8995--http://bbs.ipark.cn/archiver/tid-16059.html
    9626--xxx两个数据库:mytest和mmtest 他们都有一个info表, mytest的info表和mmtest的info的表结构是一样的,
    这两个表的数据也是一样的,所以看上去象是重复显示了一次。mysql_set_charset是设置编码 要和你的网页编码一致! 否则中文乱码
      

  2.   

    如果两个数据库的编码也是一样的
    那么就可以简略成这样:
    $conn = mysql_connect('localhost','root','root');
    $sql = "select * from mmtest.info union all (select * from mmtest.info)"; 
    $query = mysql_query($sql);
    while($re = mysql_fetch_assoc($query)){
      echo $re['id']."--".$re['url']."<br />\n";
    }
    mysql_close($conn);
      

  3.   

    如果两个数据库的编码也是一样的
    那么就可以简略成这样:
    $conn = mysql_connect('localhost','root','root');
    mysql_query("SET NAMES `GBK`");//设置编码
    $sql = "select * from mmtest.info union all (select * from mmtest.info)";  
    $query = mysql_query($sql);
    while($re = mysql_fetch_assoc($query)){
      echo $re['id']."--".$re['url']."<br />\n";
    }
    mysql_close($conn);
      

  4.   

    $conn = mysql_connect('localhost','root','root');
    mysql_query("SET NAMES `GBK`");//设置编码
    $sql = "select * from mmtest.info union all (select * from mmtest.info)";   
    $query = mysql_query($sql);
    while($re = mysql_fetch_assoc($query)){
      echo $re['id']."--".$re['url']."<br />\n";
    }
    mysql_close($conn);有点看不明白,这连接了两个数据库吗?调用了两个表的吗mmtest, mytest不用吗
      

  5.   

    数据库1 mytest 
    数据库2 mmtest
    他们都有一个名为info的表,且表结构相同。在连接数据库服务器后,不需要mysql_select_db,只要把查询语句中的表名用“库名.表名”来表示就可以了。mysql可以根据查询语句来识别是哪个库的哪个表