Jump to content

PDO with multiple databases?


cougar23

Recommended Posts

I have used PDO do make a generic method that I call to execute SQL queries to my database (myadvisor).  I have been executing my queries pulling from the STUDENT and FACULTY tables within the myadvisor database.  However, when we go to production, all the info from student and faculty will come from a seperate database (human_resources).  I was wondering how to do (and how I will have to modify my code) to execute a query pulling from both the myadvisor and human_resources databases?  In the following example of what I have currently, the APPT info will all come from the myadvisor database, while the student info will need to come from the human_resources database, where a.STUDENT would be a foreign key of human_resouces.students on the student's ID number. Thanks for the help in advance!

 

function getAppointments($facultyId) {

	require_once('../includes/DatabaseManager.inc');
	$dbManager = new DatabaseManager('localhost', 'myadvisor', 'guest', 'guest');

	//build query
	$query = 'select a.APPT_DATE, a.APPT_START_TIME, a.APPT_END_TIME, a.STUDENT, s.STUD_F_NAME, s.STUD_M_NAME, s.STUD_L_NAME
    			  from ADVISING_APPOINTMENT a, STUDENT s
    			  where a.ADVISOR = :facultyId
    			  	and a.STUDENT is not null
    			  	and a.STUDENT = s.CWID
    			  order by a.APPT_DATE asc, a.APPT_START_TIME asc';
	$queryHolders = array(':facultyId');
	$params = array($facultyId);
	$paramTypes = array(PDO::PARAM_INT);

	$results = $dbManager->doPreparedStmt($query, $queryHolders, $params, $paramTypes);
	return $results;		
}

 

class DatabaseManager {

//-----------------//
// CLASS VARIABLES //
//-----------------//
private $dbHost; 
private $dbName; 
private $dbUser;
private $dbPass;
//-------------------------------------------------------------------
//--------------//
// CONSTRUCTORS //
//--------------//
public function __construct($dbHost, $dbName, $dbUser, $dbPass) {
	$this->dbHost = $dbHost;
	$this->dbName = $dbName;
	$this->dbUser = $dbUser;
	$this->dbPass = $dbPass;
}

public function getDatabaseHost() { return $this->dbHost; }
public function setDatabaseHost($value) {$this->dbHost = value; }
public function getDatabaseName() { return $this->dbName; }
public function setDatabaseName($value) {$this->dbName = value; }
public function getDatabaseUser() { return $this->dbUser; }
public function setDatabaseUser($value) {$this->dbUser = value; }
public function getDatabasePassword() { return $this->dbPass; }
public function setDatabasePassword($value) {$this->dbPass = value; }
//-------------------------------------------------------------------
//-----------//
// FUNCTIONS //
//-----------//
/*******************************************************************
Function: doPreparedStatement
Creation Date:
Created By: 
Parameters: 
Returns:	
Description: 
********************************************************************/	
function doPreparedStmt($query, $queryHolders, $params, $paramTypes) {	

	//echo $query.'<br>';
	try {
		//establish database connection
	    $db = new PDO("mysql:host=$this->dbHost;dbname=$this->dbName", $this->dbUser, $this->dbPass);				    
	    $stmt = $db -> prepare($query);
	    
	    //bind the specified parameters
		$counter = 0;
		foreach($queryHolders as $q) {

	    	$stmt -> bindParam($q, $params[$counter], $paramTypes[$counter]);
	    	//echo 'binding '.$q.' to value of '.$params[$counter].'<br>';
	    	$counter += 1;
	    }		   
	    //execute the query and return all rows resulting from the query executed
	    $stmt -> execute();
	    //echo $stmt -> rowCount();
	    return $stmt -> fetchAll();		    
	}
	catch(PDOException $e) {
	    echo $e->getMessage();
	}
	//finally close statment and database connection
	$stmt -> close();
	$db = null;
}
//-------------------------------------------------------------------
}//end class

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.