note 52584 added to function.mysql-select-db
| From: | buzz at oska dot com | Date: | Fri, 06 May 2005 12:39:17 +0000 |
| Subject: | note 52584 added to function.mysql-select-db | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88999@lists.php.net to get a copy of this message | ||
As has been already commented, opening multiple connection handles with:
<?php
$connection_handle = mysql_connect($hostname_and_port,$user,$password);
?>
causes the connection ID/handle to be REUSED if the exact same parameters are passed in to it.
This can be annoying if you want to work with multiple databases on the same server, but don't
want to (a) use the database.table syntax in all your queries or (b) call the
mysql_select_db($database) before every SQL query just to be sure which database you are working
with.
My solution is to create a handle for each database with mysql_connect (using ever so slightly
different connection properties), and assign each of them to their own database permanently. each
time I do a mysql_query(...) call, I just include the connection handle that I want to do this call
on eg (ive left out all error checking for simplicity sake):
<?php
// none of thesehandles are re-used as the connection parameters are different on them all, despite
connecting to the same server (assuming 'myuser' and 'otheruser' have the same
privileges/accesses in mysql)
$handle_db1 = mysql_connect("localhost","myuser","apasswd");
$handle_db2 = mysql_connect("127.0.0.1","myuser","apasswd");
$handle_db3 = mysql_connect("localhost:3306","myuser","apasswd");
$handle_db4 = mysql_connect("localhost","otheruser","apasswd");
// give each handle it's own database to work with, permanently.
mysql_select_db("db1",$handle_db1);
mysql_select_db("db2",$handle_db2);
mysql_select_db("db3",$handle_db3);
mysql_select_db("db4",$handle_db4);
//do a query from db1:
$query = "select * from test"; $which = $handle_db1;
mysql_query($query,$which);
//do a query from db2 :
$query = "select * from test"; $which = $handle_db2;
mysql_query($query,$which);
//etc
?>
Note that we didn't do a mysql_select_db between queries , and we didn't use the database
name in the query either.
Of course, it has the overhead of setting up an extra connection.... but you may find this is
preferable in some cases...
----
Manual Page -- http://www.php.net/manual/en/function.mysql-select-db.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52584
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52584&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52584&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52584&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52584&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52584&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52584&report=yes
Search -- http://master.php.net/manage/user-notes.php