Please I am very new to PDO, rather a novice at PHP as well. I am currently working on a project that involves connections to many databases: MySQL, MSSQL and Oracle. So I am using the class below, with PDO, for my connection. The class code is below.
class db {
private static $objInstance;
/*
* Class Constructor - Create a new database connection if one doesn't exist
* Set to private so no-one can create a new instance via ' = new DB();'
*/
private function __construct() {}
/*
* Like the constructor, we make __clone private so nobody can clone the instance
*/
private function __clone() {}
/*
* Returns DB instance or create initial connection
* @param
* @return $objInstance;
*/
public static function getDB($DBtype, $DBindex) {
include('vars.inc.php');
if (!self::$objInstance){
$DBid = $DBindex - 1;
switch ($DBtype){
case "mysql":
self::$objInstance = new PDO("mysql:host=".$dbvars[$DBid][0].";dbname=".$dbvars[$DBid][1], $dbvars[$DBid][2], $dbvars[$DBid][3]);
break;
case "mssql":
self::$objInstance = new PDO("odbc:Driver={SQL Server};Server=".$dbvars[$DBid][0].";Database=".$dbvars[$DBid][1], $dbvars[$DBid][2], $dbvars[$DBid][3]);
break;
case "oci";
self::$objInstance = new PDO("oci:dbname=//".$dbvars[$DBid][0].":".$dbvars[$DBid][4]."/".$dbvars[$DBid][1], $dbvars[$DBid][2], $dbvars[$DBid][3]);
break;
// Add other case(s) here if another RDBMS (Relational Database Management system) is used
default:
break;
}
self::$objInstance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return self::$objInstance;
}
}
And here is the vars include file that is required by the class, I used an array cos I felt this way, new databases can be added easily to the vars file by a non-programmer over time. Of course, here, I have changed the var file values.
define('DB_SERVER', 'localhost'); define('DB_NAME', 'db1name'); define('DB_USER', 'root'); define('DB_PASSWORD', 'rootpass'); define('DB_PORT', ''); define('DB2_SERVER', 'xxx.xxx.xx.xxx'); define('DB2_NAME', 'db2name'); define('DB2_USER', 'root2'); define('DB2_PASSWORD', 'rootpass2'); define('DB2_PORT', ''); define('DB3_SERVER', 'xx.xxx.xxx.xxx'); define('DB3_NAME', db3name'); define('DB3_USER', 'root3'); define('DB3_PASSWORD', 'rootpass3'); define('DB3_PORT', ''); define('DB4_SERVER', 'xxx.xx.xxx.xx'); define('DB4_NAME', 'oracledb'); define('DB4_USER', 'root4'); define('DB4_PASSWORD', 'rootpass4'); define('DB4_PORT', '1521'); $dbvars = array( array(DB_SERVER, DB_NAME , DB_USER, DB_PASSWORD, DB_PORT), array(DB2_SERVER, DB2_NAME , DB2_USER, DB2_PASSWORD, DB2_PORT), array(DB3_SERVER, DB3_NAME , DB3_USER, DB3_PASSWORD, DB3_PORT), array(DB4_SERVER, DB4_NAME , DB4_USER, DB4_PASSWORD, DB4_PORT) );
Now the problem is that, whenever I have connected to one database and try to run my queries on another one, PDO keeps remembering the old database. But if I independently run either query, everything is fine. Can someone please help with this, or suggest a better method? :(
E.g.
include('./includes/db.class.php');
try {
$result = DB::getDB("mysql", 3)->query("SELECT myrow FROM mytable");
foreach($result as $row){
print $row['myrow'].'<br />';
}
}catch(PDOException $e){
echo $e->getMessage();
}
echo "<br />Then<br /><hr /><br />";
try {
$result = DB::getDB("mysql", 1)->query("SELECT yourrow FROM yourtable");
foreach($result as $row){
print $row['yourrow'].'<br />' ;
}
}catch(PDOException $e){
echo $e->getMessage();
}
In this case, PDO will simply keep checking DATABASE db1name for TABLE yourtable rather than check DATABASE db3name. So PDO will throw an error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db1name.yourtable' doesn't exist
You have it setup as a singleton. So your next call to Db::getDB
returns the original instance. If you want to cache the instances for the duration of the script, change $objInstance
to an array, and then instead of doing:
if (!self::$objInstance){
Do
$signature = $DBtype . $DBindex;
if (!isset(self::$objInstances[$signature])) {
Of course you'll need to change the assignment lines and the return line as well, but I think you get the idea...