Multiple mysql databases

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
lyleyboy
Forum Commoner
Posts: 27
Joined: Sat Mar 18, 2006 5:05 am
Location: Birmingham, UK

Multiple mysql databases

Post by lyleyboy »

Hi all,

I'm after some advice. I am reasonable with php mysql but not great. What I have is a quickly growing project and database size limitations (Host driven).
What I would like to do is to use two databases. At the moment I use one for all the users and photo galleries but I'd like to use a different one for the next bit of the project.

My question is can I have two running together.
If I have two comm includes at the start of the file will mysql just find the correct table from my queries.

I hope I have explained this well enough but I'm not certain. If you need any more help let me know.
webmonkey88
Forum Newbie
Posts: 20
Joined: Fri Aug 14, 2009 4:30 am

Re: Multiple mysql databases

Post by webmonkey88 »

from what i have just quickly read here : http://uk3.php.net/mysql_select_db

The mysql_query() will work on the currently selected database, if you have one user who can access both database you could from my understanding of what i have just read(not done this personally) change between the two databases with mysql_select_db(), this should enable you to work with both databases.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Multiple mysql databases

Post by onion2k »

You can connect to as many databases as you like (up to whatever the number of connections your server is limited to is).

Code: Select all

$connection_1 = mysql_connect("localhost","bob","password");
mysql_select_db($connection_1,"shop");
$connection_2 = mysql_connect("localhost","fred","password");
mysql_select_db($connection_2,"office");
$connection_3 = mysql_connect("localhost","alice","password");
mysql_select_db($connection_3,"warehouse");
 
$result_from_shop_database = mysql_query("select * from table", $connection_1);
 
$result_from_office_database = mysql_query("select * from table", $connection_2);
 
$result_from_warehouse_database = mysql_query("select * from table", $connection_3);
Post Reply