Sharing data between databases using PHP

Posted on 2011-10-20
Last Modified: 2012-06-15
I just found the answer to a question describing how you can reference data across 2 databases, it was very helpful.

My question is how can you access data from 2 different databases within 1 php page?

For example I have a php script, db_functions.php, that connects to the database Test1, I run it at the beginning of each of my pages that need to access the database, it has simple php functions that connect to the mysql database Test1. So lets say I am on page1.php and I am connecting to Test1.table1, but I have another table in another database that I need to access at the same time, lets say Test2.table2. How do I go about connecting to the second table in the second database? Is this possible? The idea would be to have redundant information in one database, and instead of recreating a table, for example users, within 4 or 5 different databases, you would keep users seperate and just access it when it is required, like for a login...

I will show my code for db_functions.php and how I connect in my page1.php.

$db = mysql_connect("","username","password");

function openDB()
	global $db;
		die("db failed: " . mysql_error());
	$db_selected = mysql_select_db('Test1', $db);
	if (!$db_selected) {
		die ("Can\'t use db : " . mysql_error());
function closeDB()
	global $db;

function runQuery($query)
	$result = mysql_query("$query");
	if (!$result) {
		return mysql_errno();
	return $result;

include 'db_functions.php';

$query1 = "select firstname, lastname from table1'";
$result = runQuery($query1);

Open in new window

Question by:bmwlaval
    LVL 24

    Expert Comment

    As long as you have access to both DB's even if you're currently at Database1 you should be able to access tables in Database2 via Database2.TableA notation
    LVL 19

    Accepted Solution

    Just open multiple DB handles:


    $db_selected1 = mysql_select_db('Test1', $db1);
    $db_selected2 = mysql_select_db('Test1', $db2);

    You can open as many as you like - just try to remember to mysql_close() all of them when you're done :)
    LVL 1

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (…
    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now