[Q] External Database interaction?

TheEdge 05 Jun, 2008
G'Day,

I have a mySQL database located on the same server as my J! 1.5 installation. Now I understand that I can integrate PHP code within the form to populate it with values and to use those values to perform some more PHP to display some results out of the external database.

However where I am struggling is how I *make* the connection to the external database. Natually JFactory::getDBO is not what I need as I want to make a connection to a *different* database than the current J! one.

So could anyone provide me with some simple code as to how to connect to the database so that I can use the database connection to:

1. Populate a drop down in the form
2. Use the same connection to perform a query on the database based on what has been selected in the forms.

I realise I could install ADODB but that does not seem right when there must be DB functionality within J! itself.

All help, pointers etc. greatly appreciated.
GreyHead 05 Jun, 2008
Hi The Edge,

You can probably adapt the Factory code to open another database object
	/**
	 * Create an database object
	 *
	 * @access private
	 * @return object JDatabase
	 * @since 1.5
	 */
	function &_createDBO()
	{
		jimport('joomla.database.database');
		jimport( 'joomla.database.table' );

		$conf =& JFactory::getConfig();

		$host 		= $conf->getValue('config.host');
		$user 		= $conf->getValue('config.user');
		$password 	= $conf->getValue('config.password');
		$database	= $conf->getValue('config.db');
		$prefix 	= $conf->getValue('config.dbprefix');
		$driver 	= $conf->getValue('config.dbtype');
		$debug 		= $conf->getValue('config.debug');

		$options	= array ( 'driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix );

		$db =& JDatabase::getInstance( $options );

		if ( JError::isError($db) ) {
			jexit('Database Error: ' . $db->toString() );
		}

		if ($db->getErrorNum() > 0) {
			JError::raiseError(500 , 'JDatabase::getInstance: Could not connect to database <br/>' . 'joomla.library:'.$db->getErrorNum().' - '.$db->getErrorMsg() );
		}

		$db->debug( $debug );
		return $db;
	}
Not tested!

Bob
Corvino 27 Jun, 2011
Hi,
what's the "Factory code"?
Thanks
GreyHead 27 Jun, 2011
Hi corvino,

This is a very old thread. What question are you trying to answer?

Which version of ChronoForms are you using? You can find the version from Site Admin | Extensions | Install/Uninstall | Components in Joomla! 1.5 or Site Admin | Extensions | Extension Manager | Manage in Joomla! 1.6.

Bob
Corvino 27 Jun, 2011
Hi,
my version is 4.0 RC1.9 on Joomla 1.6.

I need to connect with Chronoforms to an external DB.

I see your how-to here:
http://greyhead.net/how-to-docs/chronoforms-connecting-to-more-than-one-table
but it refers to "vers. 3" and there is any action "DB Connection" in version 4

After connecting to an external DB i need to show some records using an unordered list and then if the user click on an element it will show the details of the record.

Can you help me?

Thanks
GreyHead 27 Jun, 2011
Hi Corvino,

In CFv4 the DB Save action includes parameters to connect to an external database but if I understand correctly you want to read from it? Unfortunately that isn't built in.

I think that you can use the code from that Help document in a Custom Code action and it should work in V4 OK.

Bob
This topic is locked and no more replies can be posted.