-2

Will this works, or should I disconnect first?

<?php
mysql_connect('localhost','root','');
mysql_select_db('DB1');
//Code... 
//Now I need to change of db
mysql_select_db('DB2');
//More code...

I know mysql() is being deprecated, but I'm working with a old (not so much) system. Thanks!

Keoma Borges
  • 683
  • 2
  • 12
  • 27

4 Answers4

1

The documentation say yes:

Ref: http://php.net/manual/en/function.mysql-select-db.php

All queries will run on the current active database, at this case DB2.

Sets the current active database on the server that's associated with the specified link identifier. Every subsequent call to mysql_query() will be made on the active database.

GarouDan
  • 3,743
  • 9
  • 49
  • 75
1

You can use it in sql like

SELECT `database`.`table`.`value` FROM `database`.`table`
user1692333
  • 2,461
  • 5
  • 32
  • 64
1

The docs say:

Sets the current active database on the server that's associated with the specified link identifier. Every subsequent call to mysql_query() will be made on the active database.

So yes. I guess it is comparable to a use <database> command on the client.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
0

you can have more than one active connection

$db1=mysql_connect('localhost','root','');
mysql_select_db('DB1', $db1);

$db2=mysql_connect('localhost','root','');
mysql_select_db('DB2', $db2);

mysql_query("SELECT * FROM table", $db1);
mysql_query("SELECT * FROM table", $db2);
Waygood
  • 2,657
  • 2
  • 15
  • 16