Page 1 of 1

Function to connect to multiple MySQL db

Posted: Tue Feb 26, 2008 11:11 am
by CNibbana
I am trying to write a function that when called will connect to the MySQL database referenced in the function.

I use the same MySQL username as my database name so I use the same variable $name for both of them and append it with a prefix (ex. mysite_blog, mysite_forum). My problem seems to be getting the variables into the functions - which is my weak point. Here is my code:

Code: Select all

 
<?php
$host = "localhost";
$name = "PREFIXHERE";
$pass = "PASSWORDHERE";
 
function db_main   () {$name .= 'main'; db_connect();}
function db_blog   () {$name .= 'blog'; db_connect();}
function db_gallery() {$name .= 'gallery'; db_connect();}
function db_forum  () {$name .= 'forum'; db_connect();}
function db_webmail() {$name .= 'webmail'; db_connect();}
 
function db_connect($host, $name, $pass) {
    mysql_connect($host, $name, $pass) or die(mysql_error());
    mysql_select_db($user) or die(mysql_error());
}
 
The result I get tells me that the server isn't receiving the variables:

Code: Select all

 
Warning: Missing argument 1 for db_gallery(), called in /htdocs/www/index.php on line 57 and defined in /htdocs/PHP/includes/config.inc on line 16
 
Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /htdocs/www/index.php on line 58
 
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /htdocs/www/index.php on line 58
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
 
Any help would be greatly appreciated :)

Re: Function to connect to multiple MySQL db

Posted: Tue Feb 26, 2008 11:43 am
by Zoxive
Your scope is all out of whack.

I suggest reading http://us2.php.net/variables.scope.

You are also missing a Username, you have Database name, but no username.

Code: Select all

 
// Variables changed to show they are in a different scope
function db_connect($_host,$_db,$_name,$_pass){
  $Con = mysql_connect($_host, $_name, $_pass) or die(mysql_error());
  mysql_select_db($_db,$Con) or die(mysql_error());
  return $Con;
}
// Variables changed to show they are in a different scope
function db_main ($_host,$_db,$_name,$_pass) {
  $_db.= 'main';
  return db_connect($_host,$_db,$_name,$_pass);
}
// To Call it Using Variables from global scope, which you have set at the top of the page.
$Con = db_main($host,$db,$name,$pass);
 

Re: Function to connect to multiple MySQL db

Posted: Tue Feb 26, 2008 1:00 pm
by CNibbana
Thank-you for your help. As I mentioned, I am using the same name for my Username and Database name which is why I only defined one variable for the two.

[EDIT: I'm going to edit this to show two seperate variables for username and database name as a reference for anyone else looking for a similar solution]

I read the scope article and reworked my code as follows:

Code: Select all

 
function db_connect(&$name) {
    $host = "localhost";
    $user = "USERNAME";   
    $name = "PREFIX" . $name;
    $pass = "PASSWORD";
 
    $connect = mysql_connect($host, $user, $pass) or die(mysql_error());
    mysql_select_db($name, $connect) or die(mysql_error());
}
 
function db_main   () {$name = "main"; db_connect($name);}
function db_blog   () {$name = "blog"; db_connect($name);}
function db_gallery() {$name = "gallery"; db_connect($name);}
function db_forum  () {$name = "forum"; db_connect($name);}
function db_webmail() {$name = "webmail"; db_connect($name);}
 
Which is working. Feel free to point out anything I should have done differently - but I am happy with the result (it works!).

Re: Function to connect to multiple MySQL db

Posted: Tue Feb 26, 2008 1:16 pm
by Zoxive
My next concern is that you will have multiple connections open to different resource links. (The different Databases make new connections, resulting in a new Resource)

So each time you want to query something you will need to run one of your functions before hand (To connect & select the database), so the query uses that resource since your functions do not return it. (Mysql query uses the resource passed to it or the last open resource).