Sav'vannis
Sav'vannis

Reputation: 3

Connecting several MySQL databases with 1 shared user

I am trying to integrate a wiki, forum, chat and user database all on 1 site. To connect to the database we use:

<?php
$host="localhost.sitename.com";
$user="user_name";
$password="password";
$database="site_database";
$connection = mysql_connect($host,$user,$password)
or Die ("Could not connect to Server.");
$db=mysql_select_db($database,$connection)
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("site_database"); 
?>

I need it to also connect to the 3 other databases and am wondering if I need to list each one with a separate $database and again mysql_select_db line or all in one with a , in between?

$database="site_database";
$database="chat_database";
$database="wiki_database";
$database="forum_database";

and

mysql_select_db ("site_database");
mysql_select_db ("chat_database");
mysql_select_db ("wiki_database");
mysql_select_db ("forum_database"); 

OR

$database="site_database","chat_database","wiki_database","forum_database";

and

mysql_select_db ("site_database","chat_database","wiki_database","forum_database");

???

Upvotes: 0

Views: 46

Answers (3)

Kei
Kei

Reputation: 771

What you can do is create a function which connects you to the database

function connect($host, $username, $password, $database) {
     $db = mysql_connect($host, $username, $password);
     mysql_select_db($database, $db);
     return $db;
}

This is theorically what you want to do, but here are some tips:

  1. Avoid using mysql extension (you SHOULD use mysqli or PDO)
  2. Try not to switch between databases with a connection for further database compatibility (although MySQL supports database switching, not every DBMS will behave the same)

Also, if you had to perform a couple of operation you could do:

mysql_select_db('first', $db);
// Perform some queries
mysql_select_db('second', $db);
// Perform some other queries
mysql_select_db('first', $db);

// And then switch back to the first database

Upvotes: 0

Ralph Ritoch
Ralph Ritoch

Reputation: 3440

You can only have one database selected per connection. If you want to have 4 databases open and selected at the same time, even if your using the same login credentials you will need to have 4 open connections. Otherwise you will need to select the appropriate database before each MySQL query if the last MySQL query on that connection had a different database selected.

Upvotes: 2

Awlad Liton
Awlad Liton

Reputation: 9351

First: You should use mysqli Second: Try like this:

$Con1 = new mysqli ("host","user","password","database");
$Con2 = new mysqli ("host","user","password","database");
$Con3 = new mysqli ("host","user","password","database");

Then :

$PreparedStatement1 = $Con1->prepare(); 
$PreparedStatement1->bindparam('',);
$PreparedStatement1->execute();
$PreparedStatemet1->close(); 
$PreparedStatement2 = $Con2->prepare();
$PreparedStatement2->bindparam('',);
$PreparedStatement2->execute();
$PreparedStatemet2->close();

Upvotes: 0

Related Questions