note 86746 added to function.mysql-select-db
| From: | poet at malthea dot com | Date: | Sun, 02 Nov 2008 01:24:32 +0000 |
| Subject: | note 86746 added to function.mysql-select-db | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146026@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 66.207.199.35
Probable Submitter: 64.42.206.71
----
Manual Page -- http://www.php.net/manual/en/function.mysql-select-db.php
Edit -- https://master.php.net/note/edit/86746
Del: integrated -- https://master.php.net/note/delete/86746/integrated
Del: useless -- https://master.php.net/note/delete/86746/useless
Del: bad code -- https://master.php.net/note/delete/86746/bad+code
Del: spam -- https://master.php.net/note/delete/86746/spam
Del: non-english -- https://master.php.net/note/delete/86746/non-english
Del: in docs -- https://master.php.net/note/delete/86746/in+docs
Del: other reasons-- https://master.php.net/note/delete/86746
Reject -- https://master.php.net/note/reject/86746
Search -- https://master.php.net/manage/user-notes.php