Injecting into the Joomla context

Recently while working on a new Joomla component we felt it necessary to build a simple standalone php page that allowed us to experiment without impacting the core component we are developing. We found ourselves faced with an interesting dilemma. We are developing a Joomla component and our requirement is to use the build it Joomla methods to access the database; however our standalone php page will not natively have access to the Joomla environment.

To work around this we found you only need to include the following items in your standalone php page to gain access to all of the Joomla database calls.

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//GET ME INTO THE JOOMLA CONTEXT
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );
define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$mainframe =& JFactory::getApplication('site');

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//JOOMLA CONTEXT ACHIEVED
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Well that is all you need to the top of your PHP file to gain access to the Joomla 1.5 database environment.