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
https://forums.phpfreaks.com/topic/125105-pdo-with-multiple-databases/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.