How to join two tables from different databases - php

How to join two tables from different databases. One database is localhost, the other is in online.
$connectA = mysql_connect('localhost','root','','abc');
$connectB = mysql_connect('xxx','yyy','zzz','xyz');
I want to join department_name from table department which is having database abc and xyz.

You cannot as long as by join you mean using SQL JOINS. It is, because SQL query is always sent to specific database or server.
You need to send two independent queries, one for each database and then provide some logic in PHP to join them either by array_merge (full join) or some more advanced scripting.
Can't help more without seeing some code of your own.

As already has been said, it is not possible.
I would save the database connections and switch between the databases.
Just a simple example (and with mysqli)
$db1 = mysqli_connect("localhost", "my_user", "my_password", "world");
$db2 = mysqli_connect("localhost2", "my_user2", "my_password2", "world2");
And then when you want to execute a query
mysqli_query($db1, "SELECT * ...");
or other database
mysqli_query($db2, "SELECT * ...");
Save the results from your query's in arrays and then use for example array_merge to join them.
http://php.net/manual/en/function.array-merge.php

If the MySQL user has access to both databases and they are on the same host (i.e. both DBs are accessible from the same connection) you could:
Keep one connection open and call mysql_select_db() to swap between as necessary. I am not sure this is a clean solution and you could end up querying the wrong database.
Specify the database name when you reference tables within your queries (e.g. SELECT * FROM database2.tablename). This is likely to be a pain to implement.
Also please read troelskn's answer because that is a better approach if you are able to use PDO rather than the older extensions.

Use can use database federation (data virtualization) technologies like Teiid http://teiid.io and join the tables across the databases or even data from files and web services.

Related

Getting data from two different databases with single connection, is this a valid way

When I first read about mysqli_connect(), I thought we can only perform the sql operations only on the database that was passed as an argument to mysqli_connect() but today I just gave a try that, is it possible to perform join operations on tables of two different databases on my local server. So I have created another database (db_2). Amazingly it worked.
I tried googling on this topic and I have found very old answers related to mysql_connect() and mysql_select_db(). Just take a look at my code
$host="localhost";
$username="root";
$password="";
$db_name="db_1";
$con = mysqli_connect($host,$username,$password,$db_name);
$sql_query = "SELECT name FROM db_1.user as u inner join
db_2.user as us ON us.id = u.id
WHERE u.id = 102 LIMIT 1";
$result = mysqli_query($con,$sql_query);
$row = mysqli_fetch_array($result, MYSQL_ASSOC);
echo $row['name'];
Initially I thought we should make two connections to two databases, something like this to make use of two databases
$con1 = mysqli_connect($host,$username,$password,"db_1");
$con2 = mysqli_connect($host,$username,$password,"db_2");
but only using single connection It is easily able to fetch data between two databases.
My question is is this right way of joining two databases ? I know PDO is better way but out of curiosity just want to know.
Also are there any limitations? Is this prefered way when I have huge data.
Does it really slow down the performance when you grab data between two databases or is it little. Any help is greatly appreciated. Thanks
If both of the databases are on the same server and are accessible by the user you're using. You should be able to put together a query like this...
SELECT db1.table1.column1, db2.table2.column2 FROM db1.table1 JOIN db2.table2 ON db1.table1.column1 = db2.table2.column2;

connecting to multiple databases

I am using sphinx for a site search and it works great but now I am trying to connect to 2 mysql databases with the same exact structure and db2 is a continuance of db1 so all info should flow smoothly. I can easily get the results by switching the db name in the code but how can I select both at once?
here is some code im using
$CONF['sphinx_host'] = 'localhost';
$CONF['sphinx_port'] = 9312;
$CONF['mysql_host'] = "localhost";
$CONF['mysql_username'] = "user";
$CONF['mysql_password'] = "password";
$CONF['mysql_database'] = "db1";
$CONF['sphinx_index'] = "index index2";
$db = mysql_connect($CONF['mysql_host'],$CONF['mysql_username'],$CONF['mysql_password']) or die("ERROR: unable to connect to database");
mysql_select_db($CONF['mysql_database'], $db) or die("ERROR: unable to select database");
$sql = str_replace('$ids',implode(',',$ids),$CONF['mysql_query']);
$result = mysql_query($sql) or die($CONF['debug']?("ERROR: mysql query failed: ".mysql_error()):"ERROR: Please try later");
This code works fine if i enter a single mysql db into $CONF['mysql_database'] = "db1"; but I need to select from db1 and db2. Does anyone know how I can achieve this? Also I did not post the query because I don't think it is very useful it is just a simple select query and I am sure you get the idea.
If the databases are hosted on the same server, you can simply pick one arbitrarily in your call to mysql_select_db() and then qualify all table names in your SQL queries with a databasename. prefix. You can even mix and match tables from both databases in a single query. eg.
select h.lastname, h.firstname, p.petname
from db1.humans as h
left outer join db2.pets as p on p.human_id = h.id;
If you don't need to write queries that involve both databases, you can simply call mysql_select_db() again each time you need to switch databases.
If the databases are hosted on different servers, you will need 2 separate calls to mysql_connect(). In this case, make sure to pass the optional $link parameter to all mysql_ functions because the default behavior is to simply use the most recent connection, which can lead to all kinds of bugs. Since each database will have a separate connection, you will not be able to run individual queries that access both databases.

how to use sql connection in PHP to execute a query on two databases

how can i run a query that joins two tables from TWO different Databases in mssql_query or mysql_query in php
for example
$conn=mssql_connect($ip,$username,$password);
mssql_select_db("DB1",$conn);
$q="select A.name,B.ID from DB1.dbo.T1 A, DB2.dbo.T2 B where A.ID=B.ID";
$res=mssql_query($q);
how to run such query??
Just prefix the tablenames with the database name, as you are already doing.
The user login that you are using to connect to mySQL needs to have access to both databases. Without this, it is impossible.
I think something like this:
SELECT X.field1, Y.field2
FROM database1.table_a AS X
INNER JOIN database2.table_b as Y
ON X.id=Y.id
[EDITED]
Sorry I didn't finish the post, you should use mysqli http://www.php.net/manual/en/mysqli.query.php (don't worry for the constructor, put just 1 database) and run the query as a regular query. Also, like the guy in the top said, the user that makes the query must have the permissions for both tables.

Joining tables from 2 different connection strings

I need to join two tables from different MySQL (PHP) connection strings and different databases.
$conn = mysql_connect('192.168.30.20', 'user', 'pass');
$conn2 = mysql_connect('anotherIPHere', 'user2', 'pass2');
$db = mysql_select_db('1stdb', $conn);
$db2 = mysql_select_db('2nddb', $conn2);
If I were using the same connection I would just prefix the tables with the db names, such as database1.table1.column and database2.table2.column2, but since I'm using two completely separate connection strings the MySQL Query does not know which connection string to use, thus the resource is not usable.
I've read a ton of resources that show how to use two databases, from the SAME connection string and that is working fine, but I can't find anything related to multiple connection strings and databases.
Thanks
There's no way to do it in the query. About the only possibility is if you use a MySQL FEDERATED Table which lets you access tables on remote servers.
You can't do it database-server-side, that's for sure. You'll have to download the filtered rowset for each table you want to join against, then iterate manually in-code to join the two sets of rows where you want.

How do I construct a cross database query in PHP?

In our last episode (How I constructed a cross database query in MySQL) I learned how to construct a cross database query in MySQL. This worked great but when our hero tried to use this newfound knowledge in PHP he found his best friend FAIL waiting for him.
I took a look at mysql_select_db for PHP. This seems to imply that if I want to use MySQL with PHP, I have a couple of choices:
Use mysql_select_db but be stuck with only using one db at a time. This is our current setup and putting a database as a namespace identifier doesn't seem to work (it works fine in the MySQL shell so I know it's not a problem with our MySQL server setup).
Don't use mysql_select_db. From some of the examples I've seen, this seems to mean that I have to specify the db for every query that I make. This makes sense since I haven't used mysql_select_db to tell PHP what db I want to access. This also makes sad since I don't want to go through all my code and prepend a db name to every query.
Is there something better than this? Is there a way for me to do a cross db MySQL query in PHP without having to something crazy like (2)?
CLARIFICATION: None of the proposed answers actually let me do a cross db query. Instead, they allow me to access two different DBs separately. I want a solution that allows me to do something like SELECT foreign_db.login.username, firstname, lastname from foreign_db.login, user where ... NOT just make different queries to different DBs. For what it's worth, (2) doesn't work for me.
You will need your databases to run on the same host.
If so, you should be able to use mysql_select_db on your favourite/default db and manually specify a foreign database.
$db = mysql_connect($hots, $user, $password);
mysql_select_db('my_most_used_db', $db);
$q = mysql_query("
SELECT *
FROM table_on_default_db a, `another_db`.`table_on_another_db` b
WHERE a.id = b.fk_id
");
If your databases run on a different host, you won't be able to join directly. But you can then make 2 queries.
$db1 = mysql_connect($host1, $user1, $password1);
$db2 = mysql_connect($host2, $user2, $password2);
$q1 = mysql_query("
SELECT id
FROM table
WHERE [..your criteria for db1 here..]
", $db1);
$tmp = array();
while($val = mysql_fetch_array($q1))
$tmp[] = $val['id'];
$q2 = mysql_query("
SELECT *
FROM table2
WHERE fk_id in (".implode(', ', $tmp).")
", $db2);
After reading your clarification, I am under the impression that you actually want to query tables residing in two separate MySQL server instances. At least, your clarification text:
SELECT foreign_db.login.username, firstname, lastname from foreign_db.login, user where
suggests that you want to run one query while being logged in as two users (which may or may not reside on the same mysql server instance).
In your question, you said you wanted to query data from two different databases, but it is important to realize that one MySQL instance can have many, many databases. For multiple databases managed by the same mysql instance, the solution proposed in the question you linked to simply works: just prefix the table name with the name of the databases, separating database and table names with a dot: <db-name>.<table-name>.
But, like i pointed out, this only works if:
all databases you access in one query reside on the same server - that is, are managed by the same MySQL instance
the user that is connected to the database has the right privileges to access both tables.
Scenario1: databases on same host: grant appopriate privileges and qualify table names
So if the tables actually reside on the same mysql instance, there is no need for a second login or connection - simply grant the database user you use to connect to the datbase the appropriate privileges to select from all tables you need. You can do that with the GRANT syntax, documented here: http://dev.mysql.com/doc/refman/5.1/en/grant.html
For example, GRANT SELECT ON sakila.film TO 'test'#'%' will allow the user test#% to select data from the film table in the sakila database. After doing that, said user can refer to this table using sakila.film (so-called qualified table name), or if the current database is set to sakila, simply as film
Scenario2: databases managed by different MySQL instances: FEDERATED engine
If the tables you want to access are actually managed by two different MySQL instances, there is one trick that may or may not work, depending on your configuration. Since MySQL 5.0 mysql supports the FEDERATED storage engine. This lets you create a table that is not actually a table, but a peephole to a table on a remote server. This engine is documented here: http://dev.mysql.com/doc/refman/5.1/en/federated-storage-engine.html
For example, if you know there is this table in the misc database on the remote host:
CREATE TABLE t (
id int not null primary key
, name varchar(10) not null unique
)
you can make a local 'pointer' to that remote table using this:
CREATE TABLE t (
id int not null primary key
, name varchar(10) not null unique
)
ENGINE = FEDERATED
CONNECTION='mysql://<user>#<remote-server>:<remote-port>/misc/t';
Unfortunately, the FEDERATED engine is not always available, so you have to check first if you can even use that. But suppose it is, then you can simply use the local table t in your queries, just like any other table, and MySQL will communicate with the remote server and perform the appropriate operations on the physical table on the other side.
Caveat: there are several optimization issues with FEDERATED tables. You should find out if and to what extent these apply to you. For instance, applying a WHERE to a federated table can in many cases result in the entire table contents being pullled over the wire to your local server, where the actual filtering will be appplied. Another issue is with table creation: you have to be very sure that the definitions of the federated table and the table it is pointing to match exacty, except for the ENGINE clause (and CONNECTION). If you have for example a different character set, the data may arrive completely garbled after travelling over the wire.
If you want to use FEDERATED tables, do read this article http://oreilly.com/pub/a/databases/2006/08/10/mysql-federated-tables.html to decide if its right for your particular use case.
If you think you do need it, I have a utility to create federated tables here: http://forge.mysql.com/tools/tool.php?id=54
Scenario3: can't use FEDERATED, but tables on different MySQL instances
Finally, if you have tables on different MySQL instances, but cannot for some reason use the federated table engine, your a out of luck I'm afraid. You are simply going to have to execute queries to both MySQL instances, receive the results and do something intelligent with it in PHP. depending on your exact requirements, this may be a perfectly viable solution
I guess you need to decide for yourself which part of my answer best appeals to your problem, and add a comment in case you need more help. TIA Roland.
A solution might be to :
use mysql_select_db to select the "default" (i.e. most used) database
and specify the DB name only in queries that have to work with the "second" (i.e. least used) database.
But this is only a viable solution if you have one DB that's more used than the other...
Out of curiosity : did you try establishing several connections to your DB server -- i.e. one for each database ?
You might be able to :
connect to the first DB with mysql_connect, and, then, select the first DB with mysql_select_db
and, then, connect to the second DB, passing true for the new_link parameter of mysql_connect if necessary, and, then, selecting the second DB with mysql_select_db
Then, work with the connection identifier returned by the first, or second, call to mysql_connect, depending on which DB you want to issue queries.
As a sidenote : the "best" / "cleanest" solution would be not using mysql_* functions directly, but working with some kind of ORM framework, that would have the ability to work with several DB connections at the same time (not sure, but maybe Doctrine can do that -- it's a real good ORM)
I set up tables in separate test databases as follows:
mysql> use test;
mysql> create table foo as select 42 as id from dual;
mysql> create database test2;
mysql> use test2;
mysql> create table bar as select 42 as id from dual;
I ran the following PHP script with MySQL 5.1.41 and PHP 5.3.1 on Mac OS X:
<?php
$link = mysql_connect('localhost', 'root', 'XXXX')
or die('There was a problem connecting to the database.');
mysql_select_db('test');
$sql = "SELECT * FROM foo JOIN test2.bar USING (id)";
if (($result = mysql_query($sql)) === FALSE) {
die(mysql_error());
}
while ($row = mysql_fetch_array($result)) {
print_r($row);
}
This test succeeds. The result is the join between the two tables in separate databases.
You should always be able to select from table(s) qualified by their respective database names in SQL. The mysql API in PHP does not restrict you to querying one database.
You should always be able to omit the database qualifier for the "current" database, which you declare with mysql_select_db().
Maybe this is the code that you want
//create links
$link1 = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$link2 = mysql_connect('localhost', 'mysql_user', 'mysql_password');
//set db on every link
mysql_select_db('foo', $link1);
mysql_select_db('bar', $link2);
//do query with specified link
$result1 = mysql_query($query1,$link1);
$result2 = mysql_query($query2,$link2);
Note that we didn't do a mysql_select_db between queries , and we didn't use the database name in the query either.
Whenever you are SELECTing from multiple tables you have to sepcify an alias. So it's pretty simple from there:
SELECT
a.id, a.name, a.phone,
b.service, b.provider
FROM
`people` AS a,
LEFT JOIN
`other_database`.`providers` AS b ON a.id = b.userid
WHERE
a.username = 'sirlancelot'
As others on this page have mentioned, the database must be on the same host and instance. You cannot query a database from another server with this syntax.
The less verbose option you have is provided by the MySQL Manual itself:
The following example accesses the
author table from the db1 database and
the editor table from the db2
database:
USE db1;
SELECT author_name, editor_name FROM author, db2.editor
WHERE author.editor_id = db2.editor.editor_id;
You can use option two: "Don't use mysql_select_db" and then use mysql_db_query instead of mysql_query ... which is a simple find and replace.
Best of luck!
I've just tried this simple code in my computer and it works perfectly:
<?php
$conn = mysql_connect('localhost', 'root', '....');
mysql_select_db('aliro');
$sql = 'select * ' .
'from aliro_permissions a ' .
'left join cmsmadesimple.cms_permissions b on a.id=b.permission_id ';
$res = mysql_query($sql)
or die(mysql_error());
while($row = mysql_fetch_assoc($res)){
print_r($row);
}
?>
(Of course, the query itself is meaningless, it's just an example.)
So I can't really see what your exact problem is. If you want a syntax that's simpler that this, you'll have to provide an example of what kind of SQL you want to write.

Categories