PDO connection handlers

What I am about to ask for is quite horrible, I know, still am just weigh my options and this is one of them.
Is there a way to extract/access ONLY the DB connection from a PDO w/o going through a PDOStatement?

A while back, in my novice student days, I wrote a CMS script using procedural PHP and mysli functions.

mysqli_connect($host,$UserAcc,$pass)

Sometime later , in my novice days, I was able to streamline my code ( with minimal work ) by using a singleto/registry pattern to store my DB handlers. Even though my functions still used mysqli my code automatically reused DBhandles by accessing the registry object’s methods.


function DBconn($host=HOST, $UserAcc=UACC,$pass=UPASS){
 		 $connect= mysqli_connect($host,$UserAcc,$pass);
		 return $connect;
 } // opens DB connection handle
      class DBReg{
                protected static $conns= array(); // storage for  DB connection handles
	        public static function getConn($n=DBNAME){
		     if (!isset(self::$conns[$n])){return self::addConn($n);}
		     return self::$conns[$n] ;
                 }// returns a DB connection handle from the registry
	   public static function addConn($n=DBNAME, $UserAcc=UACC,$pass=UPASS,$host=HOST){
		 self::$conns[$n]=DBconn($host,$UserAcc,$pass);
		 return self::$conns[$n];
	       }
        }
	$db=mysqli_select_db(DBReg::getConn($cn),$cn);


Recently, I rewrote the script to employ PDOs … works great. But I still have to keep the old methods in there for backwards compatibility with old templates that were using procedural or hybrid code. :confused:

it occurs to me that I could go back and tweak some of my previous methods/function to act as a shell, returning the same type of data as before but obtain their results from my new PDO driven registry.

THE ISSUE is that the old code expects DB handle and not an actual PDO or PDOStatement, so my question how can I get just the DB connection handle ( the equivalent of mysqli_connect($host,$UserAcc,$pass); ) from a PDO… I need to get just the handle and NOT a PDOStatement or or PDO handle object, that is if that can be done at all.

Thanks in advance!

You can’t. PDO is completely different paradigm from the old mysqli_* stuff.

Your options are 1. keep everything as is and keep using mysqli_*, or 2. rewrite everything to PDO or 3. write some sort of inbetween-layer that converts the old stuff to the new stuff. Depending on your setup option 3 may or may not be possible.

I was trying to do #3… hoping that since I could merely use the existing class/function as my wrapper so as not to have to rewrite any templates. But I guess since my previous functions returned only the DB handler ( other scripts and functions actually did the query, retrieval, etc.) I will just have to go back and redo everything as a PDO.

[ot]Why do you feel the need to move away from MySQLi? That is a supported extension and it’s perfectly allowed to keep writing code that uses it, with either the “procedural” or “OO” APIs.

It’s a completely different beast from the deprecated MySQL extension.[/ot]