Класс БД остается обычным классом, это только часть класса БД
класс dbManager
{
//DB Connections
private $Internal_db_con = null;
private $Business_db_con = null;
//DB Connection Parameters
private $host=null;
private $user=null;
private $pass=null;
//DB Names
private $internal_db_name=null;
private $business_db_name=null;
//
private $result = null;
// only one object created
public function __construct()
{
include_once __SITE_PATH . 'includes/db_config.inc';
список ($ this-> host, $ this-> user, $ this-> pass, $ this-> internal_db_name, $ this-> business_db_name) = $ db_conf;
}
//set internal database connection
private function setInternalDBCon()
{ debug('setInternalDBCon called');
try{
# MySQL with PDO_MYSQL
$dbq="\"";
$this->Internal_db_con = new
PDO ("mysql: host =". $ This-> host. "; Dbname =". $ This-> internal_db_name, $ this-> user, $ this-> pass,
массив (PDO :: MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$this->Internal_db_con->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo 'Error connecting to MySQL: ' .$e->getMessage();
}
}
//set business database connection
public function setBusinessDBCon()
{
try{
# MySQL with PDO_MYSQL
$dbq="\"";
$this->Business_db_con = new
PDO ("mysql: host =". $ This-> host. "; Dbname =". $ This-> internal_db_name, $ this-> user, $ this-> pass,
массив (PDO :: MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$ this-> Business_db_con-> setAttribute (PDO :: ATTR_ERRMODE, PDO :: ERRMODE_EXCEPTION);
}
поймать (PDOException $ e)
{
echo 'Ошибка подключения к MySQL:'. $ e-> getMessage ();
}
}
/*check if a row exists in a safe way that avoids sql atack*/
public function uniqueRowExists($dbcon, $table,$col_name, $data)
{
//check if connection is set
if($this->{$dbcon.'_db_con'}==null) //dynamic variable call
$this->callMyFunc( 'set'.$dbcon.'DBCon'); //dynamic function call
....................... и т.д. ....
//prepare
$stmt = $this->{$dbcon.'_db_con'}->prepare($sql);
$stmt->execute($data);
................. и т. Д.