Page 1 of 1

Issue connecting to multiple database sources using ADODB

Posted: Tue Jul 01, 2008 1:56 am
by SMooTH
Hi all. First time caller, long time listener.

I am working on a project using an older version of CMS Made Simple ( I think its 1.02 ) and I'm encountering an issue when I create a 2nd connection to a different database ( both mysql ).

DB connection 1 connects to the default database where all the CMS data resides.

DB connection 2 connects to a different database to gather some information to display in a form dropdown list.

When the page processes, it displays everything fine up to the point of the form.
Then it displays the form fine ( including the drop down list with data from the 2nd database )
Then it continues to display remaining page elements, until it comes to a smarty element in the template like a global_content or something, and then returns the following error message :

string(77) "Smarty error: unable to read resource: "globalcontent:site_footer_plain"" string(77) "Smarty error: unable to read resource: "globalcontent:site_footer_plain"".

When I comment out the calls to the DB2 connection, the page displays fine (except there is no data in the dropdown list ).

Here is the code I am using to create the DB2 connection:

$db2_hostname = 'localhost';
$db2_username = 'username';
$db2_password = 'password';
$db2_name = 'db2_name';

$db2_obj =  &ADONewConnection('mysql', 'pear:date:extend:transaction');
$connect_result = $db2_obj->NConnect($db2_hostname,$db2_username,$db2_password,$db2_name);

if (FALSE == $connect_result)
{
die ('Database Connection failed');
}
$db2_obj->SetFetchMode(ADODB_FETCH_ASSOC);
$my_db2 = &$db2_obj;
return $my_db2;

I am still learning my way around this system, as I have taken over this project form someone else, so any help would be appreciated.

Thanks.

Re: Issue connecting to multiple database sources using ADODB

Posted: Tue Jul 01, 2008 2:48 am
by SMooTH
Okay... so after a bit of fiddling, I found the following code ( slightly altered - change Connect to NConnect ) which seems to do the job just fine. Not sure why my previous attempts to use NConnect did not result in a working code, but it works now, so I'm not going to complain!

For those whom may need/want it, please see the connection code below:

Code: Select all

$sar =&  ADONewConnection('mysql', 'pear:date:extend:transaction');

$connect_result = $sar->NConnect('localhost', 'username', 'password', 'db2_name'); // connected with valid info different from config.php
if (FALSE == $connect_result)
   {
      echo ('Database Connection to SAR failed');
   }

// Creates an empty array to hold the results
$results= array();
		
// Create the SQL Query and Retrieve the rows
$query = "SELECT * FROM table_in_db2";

$dbresult = & $sar->Execute($query);

// Populate the rows and return an array to the caller
while($dbresult && $row = $dbresult->FetchRow())
{
	$results[$row['fieldname']] = $row['fieldname'];
}
Not sure how to close this thread down, but it can be closed.

Cheers!  ;D