get DB handler in CodeIgniter for stand-alone Libraries

We were working on a project where some developers were working with CI and were developing libraries there, and some developers were developing stand-alone libraries. But both groups were working on the same project.

At some point we needed all those stand-alone libraries to beĀ  used in CI as well.
For common code it’s not a big deal, the only problem which took a little bit of time to figure out was using the DataBase. We didin’t want to make new DB connections, so how could we use the current connection, inside an stand-alone code.

The answer is pretty easy, CI’s DB class has a non-documented attribute which returns back the DB handler.

$this->db->conn_id

or if you have multiple DB cnnections that would be :

$this->dbr->conn_id;
$this->dbw->conn_id;

and then you can easily pass the DB connection to regular mysql_query function like this :

mysql_query( string $query[,resource $link_identifier] );

and as an example :

mysql_query( 'SELECT * FROM `table1`', $this->dbr->conn_id );