1

I tried to connect two databases in single php file and tried to retrieve data by joining tables of both databases. Here is my code

require_once "connect.php";

require_once "connect_college.php";

$sql = "select * from college_db.students join mini-project.exam on students.adno=exam.adno and students.receipt=exam.receipt ";

 if (!mysql_query($sql))
              {

               die('Error');
              }
              else
            { 

              header("Location: generate.php"); 
              exit();
            }

Mysql query is not running. college_db and mini-project are the two databases.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Niyas
  • 505
  • 1
  • 6
  • 17
  • You cannot join data from 2 different database connections or servers you will have to run a select query on database server A get the result into an array or w/e then run your second query on database server B. – Prix Mar 27 '14 at 05:32
  • 2
    possible duplicate of [How do you connect to multiple MySQL databases on a single webpage?](http://stackoverflow.com/questions/274892/how-do-you-connect-to-multiple-mysql-databases-on-a-single-webpage) – i'm PosSible Mar 27 '14 at 05:38
  • what you have in `mysql_error()` ? – Awlad Liton Mar 27 '14 at 05:47

2 Answers2

0

Try like

$sql = "select college_db.students.*,mini-project.exam.* from college_db.students 
        join mini-project.exam 
        on college_db.students.adno=mini-project.exam.adno 
        and college_db.students.receipt=mini-project.exam.receipt ";
GautamD31
  • 28,552
  • 10
  • 64
  • 85
  • That is if both databases are within the same server and connection. – Prix Mar 27 '14 at 05:33
  • @Prix if you see my ans,I have mentioned those DB names also and it will work. – GautamD31 Mar 27 '14 at 05:41
  • Only if he is connecting to a single server or both of his databases are within the same server which from his 2 include seems he is connecting to 2 different servers which in that case it would not work given each database is on a different server. – Prix Mar 27 '14 at 05:42
0

If to do like this? Not will work?

 $link1 = mysql_connect('localhost', 'mysql_user', 'mysql_password');


 $link2 = mysql_connect('localhost2', 'mysql_user2', 'mysql_password2');

 mysql_select_db('database1', $link1);

 mysql_select_db('database2', $link2);

 $res1=mysql_query($query1,$link1);

 $res2=mysql_query($query1,$link2);
Michael
  • 1,089
  • 1
  • 11
  • 28