1

this is what i currently do: i create 2 connections:

$con_1 = mysql_connect('xxxxxxxx', 'db1', 'xxxxx') or die(mysql_error());
$con_2 = mysql_connect('xxxxxxxx', 'db2', 'xxxxx') or die(mysql_error());

the before each query, i do

mysql_select_db('db1', $con_1);
... query

but apparently that doesn't work. mysql_query requires me to pass in the $con_1 as well. so If I have to pass in the connection, may i skip the step of calling mysql_select_db all together?

OR, can I just call each mysql_select_db in the beginning and later when I call the query functions, just pass in the $con_x, and not have to worry about selecting the db?

1
  • Why are you still using mysql_* functions? They're deprecated. Commented May 9, 2013 at 0:17

2 Answers 2

1

The database selection is associated with the connection resource. So you can select it once for each connection.

If you don't call mysql_select_db at all, all your queries will need to specify explicit database prefixes before all the tables, e.g. select * from db1.table ....

Sign up to request clarification or add additional context in comments.

Comments

1

so If I have to pass in the connection, may i skip the step of calling mysql_select_db all together?

You'll have to call it once after establishing the connection


But, like @Barmar mentioned in his answer, you wont even need to call mysql_select_db() and and you don't need to have two seperate connections for that. You'll just have to use fully qualified table names in your queries, like dbname.tablename. Check this example:

$con = mysql_connect(...);

mysql_query('SELECT * FROM `db1`.`foo`');
mysql_query('SELECT * FROM `db2`.`bar`');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.