Function to connect to multiple MySQL db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CNibbana
Forum Newbie
Posts: 2
Joined: Tue Feb 26, 2008 10:51 am

Function to connect to multiple MySQL db

Post 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 :)
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Function to connect to multiple MySQL db

Post 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);
 
CNibbana
Forum Newbie
Posts: 2
Joined: Tue Feb 26, 2008 10:51 am

Re: Function to connect to multiple MySQL db

Post 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!).
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Function to connect to multiple MySQL db

Post 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).
Post Reply