从不同的数据库加入表 [英] Joining Tables from different Database

查看:110
本文介绍了从不同的数据库加入表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从同一服务器中两个不同数据库的两个表中提取数据。
我使用此代码

I am trying to fetch data from two tables in two different database in a same server. I am using this code

<?php

    $host='localhost';
    $root='root';
    $password='mypass';
    $db=mysql_connect($host,$root,$password) or die('unable to connect database');
    $dbname='BUSMSTR_10';
    mysql_select_db($dbname,$db) or die(mysql_error($db));

    $db2=mysql_connect($host,$root,$password) or die('unable to connect database');
    $dbname='BULIB_Info';
    mysql_select_db($dbname,$db2) or die(mysql_error($db2));

    $sql="SELECT 
        m_Student.Stud_Name_Form AS Stud_Name_Form,
        m_Student.Enrl_no AS Enrl_no,
        m_Subject.Sub_name AS Sub_name
    FROM 
        BUSMSTR_05.m_Student m_Student
    INNER JOIN
        BULIB_Info.m_Subject m_Subject
    ON
        m_Student.Sub_ID=m_Subject.Sub_ID

    LIMIT 10";

$rs=mysql_query($sql);

    while($row=mysql_fetch_assoc($rs)){
        echo $row['Stud_Name_Form']."|||||".$row['Enrl_no']."|||||".$row['Sub_name']."<BR>";
        }

但我收到错误。
它的错误....
和我将如何解决这个问题

But I am getting error. whats wrong with it.... and how will I fix this??

错误msg ----

Error msg----

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/budcc/html/student/PHP/Student.php  on line 25


推荐答案

假设它们驻留在同一个服务器上)就像使用table.field指定字段一样,也可以使用database.table.field下面是两个数据库连接的示例:

It's really not difficult to join seperate databases (assuming they reside on the same server) Just like you would specify fields using the "table.field", you can also use "database.table.field" Below is an example of a two database join:

$sql="SELECT db1.table1.somefield, db2.table1.somefield FROM db1.table1 INNER JOIN db2.table1 ON db1.table1.someid = db2.table1.someid WHERE db1.table1.somefield = 'queryCrit';"

你只需要写你的查询就像你在一个数据库中工作,只需使用点

You simply write you query just like you would if you were working in one db, just use the dot notation to specify your databases as well.

至于你的问题,我不认为你在表名之前添加数据库名称

As far as your problem goes i dont think you are adding database names before tables names everywhere.Try that.

这篇关于从不同的数据库加入表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆