Accessing an external database from a joomla module

Have a programming question regarding your component, plug-in, extension or core hacks? Have an interesting tidbit, FAQ or programming tip you’d like to share? This is the place for you.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Post Reply
Techbot
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed Apr 05, 2006 1:12 pm

Accessing an external database from a joomla module

Post by Techbot » Fri Mar 21, 2008 2:38 am

On a joomla 1.14 site


the code is fairly straightforward,

Code: Select all

<?php

defined( '_VALID_MOS' ) or
    die( 'Direct Access to this location is not allowed.' );

$conn = mysql_connect(localhost,JoeBlogs,secret) ;

mysql_select_db(external_database) ; 

$result = mysql_query("SELECT artist, title from historylist ORDER BY historylist.date_played DESC");

list($title,$artist)= mysql_fetch_row($result);
 	 
	 echo $title ;   echo "<br>" ;
	  echo $artist ; echo "<br>" ;
		 
 ?>
It works but breaks all following connections with the jos_database (as far as I can tell) ie any modules after this one stop working.

Is it because I need to make the external call, some kind of instance?.

Any help would be appreciated, this is my first module.

User avatar
Rogue4ngel
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 199
Joined: Sun Nov 26, 2006 10:46 pm
Location: New York

Re: Accessing an external database from a joomla module

Post by Rogue4ngel » Fri Mar 21, 2008 5:35 am

Greetings. I do know there has been some discussion on connecting to external databases. Here's a tip on connecting to an external db:
http://dev.joomla.org/component/option, ... _database/

Not sure if it will help, but it can't hurt.
If you're not a part of the solution, you're a part of the problem.

Techbot
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed Apr 05, 2006 1:12 pm

Re: Accessing an external database from a joomla module

Post by Techbot » Fri Mar 21, 2008 3:49 pm

Thanks Rogue.

This is my version of the above code translated to joomeese. Unlike the above which works but breaks joomla database connectivity, this fails to work at all. No doubt a typo or some syntax. I've done some decent searching and beyond the link you provided, there is actually nothing else in the forums.

I could move the external table into joomla, but I won't have learned anything as a result and it makes my next venture impossible.

thanks

Code: Select all

<?php

defined( '_VALID_MOS' ) or
    die( 'Direct Access to this location is not allowed.' );
	
$option['driver']   = 'mysql';            // Database driver name
$option['host']     = 'localhost';    // Database host name
$option['user']     = 'joebloggs';       // User for database authentication
$option['password'] = 'secret';  // Password for database authentication
$option['database'] = 'external database';      // Database name
$option['prefix']   = '';             // Database prefix (may be empty)

$database = & JDatabase::getInstance( $option );


$sql= "SELECT artist, title from historylist ORDER BY date_played DESC" ;

$database->setQuery( $sql );

$category = $database->loadRow();

 
echo $category ;
	
		  echo "<br>" ;
		  
 ?>
	

User avatar
Rogue4ngel
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 199
Joined: Sun Nov 26, 2006 10:46 pm
Location: New York

Re: Accessing an external database from a joomla module

Post by Rogue4ngel » Fri Mar 21, 2008 5:48 pm

Something I may do some research on myself. Seems to be something that a lot of people would use. I am sure someone out there has done it (a few bridges out there that work). I'll see what I can dig up.

Techbot
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed Apr 05, 2006 1:12 pm

Re: Accessing an external database from a joomla module

Post by Techbot » Fri Mar 21, 2008 6:55 pm

I'm guessing this issue is only relevant to modules not components. I'm using one of the custom code components to access the same database and it works fine.

Is it possible getinstance is a 1.5 only function?
****************************
getInstance (line 194)

Returns a reference to the global Database object, only creating it if it doesn't already exist.

The 'driver' entry in the parameters array specifies the database driver to be used (defaults to 'mysql' if omitted). All other parameters are database driver dependent.

* return: A database object
* since: 1.5

JDatabase &getInstance ([array $options = array()])

* array $options: Parameters to be passed to the database driver

http://api.joomla.org/Joomla-Framework/ ... etInstance
****************************





Does my joomlise sql look correct? ie most of the code should be the same as if, in the joomla database. I'm guessing the only different between an internal and external is the line

$database = & JDatabase::getInstance( $option );

unfortunately a search for external database leads to this post. :)

I'm gonna check the fabrik module code to see if I can find any hints there. I'll post anything I find.

Thanks again rogue.

User avatar
Rogue4ngel
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 199
Joined: Sun Nov 26, 2006 10:46 pm
Location: New York

Re: Accessing an external database from a joomla module

Post by Rogue4ngel » Sat Mar 22, 2008 12:30 am

What you have looks ok to me. I always get messed up when I start going back and forth trying to figure out what's passing by reference. Probably why I didn't become a career coder ;).

Sounds like you've got a lead to follow. Glad to hear about what you come up with, if anything. I haven't found anything yet, but there is a way around this. Perhaps someone may shed some light...
If you're not a part of the solution, you're a part of the problem.


Post Reply