Friday, April 07, 2006

Fixing Multiple Database bug in adoDB

popular data access layer for php, adoDB can support multiple databases from different provider at time, but not from same provider. what I mean is if you instantiate two adoDB connection like this

$db1 = &NewAdoConnection (“mysql”);
$db1 = &NewAdoConnection (“oracle”);

then you can run queries simultaneously from these connections. but if you want to access two database from same provider like this

$db1 = &NewAdoConnection (“mysql”);
$db1 = &NewAdoConnection (“mysql”);
$db1->PConnect(logininfo_for_first_database)
$db2->PConnect(logininfo_for_second_database)

then only last action will remain active and if you use $db1->execute(), it will actually works on second database. if you want to access any table from first database, it will generate an error. I think this is an internal bug in adoDB.

How to fix this bug?

if you open adodb-mysql.inc.php and goto function _connect() you will find this line at the end

1. if ($argDatabasename) return $this->SelectDB($argDatabasename);

now goto SelectDB function and see what it contains actually.

// returns true or false
function SelectDB($dbName)
{
$this->databaseName = $dbName;
if ($this->_connectionID) {
return @mysql_select_db($dbName,$this->_connectionID);
}
else return false;
}

so it basically returns a boolean value, mysql connection link is stored in $this->_connectionID variable. Now modify this code like this

1. add this line at the declaration section of adodb-mysql.inc.php

var $__db = array();

2. modify the SelectDB function like this

// returns true or false
function SelectDB($dbName)
{
$this->databaseName = $dbName;
if ($this->_connectionID) {
@mysql_select_db($dbName,$this->_connectionID);
$this->__db[$this->_connectionID] = $dbName;
return $this->_connectionID;
}
else return false;
}

3. Now open the _query function and modify it like this

// returns queryID or false
function _query($sql,$inputarr)
{
//global $ADODB_COUNTRECS;
//if($ADODB_COUNTRECS)
$this->SelectDB($this->__db[$this->_connectionID]);
return mysql_query($sql, $this->_connectionID);
//else return @mysql_unbuffered_query($sql,$this->_connectionID);
// requires PHP >= 4.0.6
}

That’s it!!, now your adoDB supports multiple databases from same provider.

0 Comments:

Post a Comment

<< Home