views:

82

answers:

3

I have 2 databases. Users database and purchases database. Each database has different user and password.

If i want to execute a query to call both databases. How do I connect the databases?

$db = mysql_select_db(??????);
+1  A: 

You don't have to care which db you select since you are giving to MySQL the database name in the queries.

i.e

SELECT * FROM db.table, db2.table

So whatever database you have selected it won't change a thing.

RageZ
but each database has different user and password, how to solve that? Contact my web hosting provider, ask them to give me a super admin user that can access all database i create?
mysqllearner
you have to make sure with your hosting company, that both of your database are located on the same server, also you have to ask them to give you an user which have access to both DBs.
RageZ
last comment I would get those two database in one place because you are going to have problems I think.
RageZ
A: 

call mysql_connect() twice and store the result in a variable. that variable can then be sent to just about every mysql_* call in php to tell you which one you are referring two. Look at the second argument of mysql_select_db() and you will understand.

Edit: This way, you will of course not be able to use them both in the same query but unless your database users have granted access to both, I think it is your only option.

Fredrik
+1  A: 
$db1 = mysql_connect($host1, $user1, $pass1);
mysql_select_db($db1, 'database1');

$db2 = mysql_connect($host2, $user2, $pass1);
mysql_select_db($db2, 'database2');

$query = "SELECT * FROM test";

mysql_query($db1, $query);
mysql_query($db2, $query);

EDIT: Ok, now I understand the problem (reading the comments to the other answers): If you don't have the permission, you will not be able to do a statement which connects 2 databases. If you have a user, which has permission to select from both databases, it should be possible by

SELECT * FROM db1.table, db2.table
Pesse
I am trying to grant all the privileges to 1 user. Having problem finding root password to activate mysql console. Grrr...
mysqllearner