note 86746 deleted from function.mysql-select-db by danbrown

From: Date: Wed, 17 Jun 2009 14:42:02 +0000
Subject: note 86746 deleted from function.mysql-select-db by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156992@lists.php.net to get a copy of this message
Note Submitter: poet at malthea dot com ---- Expanding on what matsko at rogers dot com said, If using a database user account that is the same across all databases, you only need to open a single connection and it will still let you access the multiple databases. Also, a user might not want to open a persistent connection. I found that you don't need to, so mysql_connect is used instead of mysql_pconnect. <?php // A single administrative user with access to both databases $handle = mysql_connect($host,$user,$pass); // Then you could do this: mysql_query("SELECT * FROM database1.table ",$handle); mysql_query("SELECT * FROM database2.table ",$handle); /** OR **/ $handle = mysql_connect($host,$user,$pass); // using first database for the following query(queries) mysql_query("USE database1",$handle); mysql_query("SELECT * FROM table"); // use the second database for the following query(queries) mysql_query("USE database2",$handle); mysql_query("SELECT * FROM table"); ?> So you can use the same connection for both databases.. the problem with the second method (using USE to select a database) is that you can confuse which table is which. The easiest is to just prepend the database name to the table (database.table). The benefit to using the first method may be if you have multiple applications with identical tables and fields that you want to easily switch between.

« previous php.notes (#156992) next »