Im currently writing a script that will be executed as a cronjob to perform some calculations using values from the joomla database, because this script will not be accessible through joomla as a plugin, etc. I need to do a DB with it.
What I'm trying to do is use the Joomla framework to do all the work (connection, requests, etc.) for security as well as portability (instead of having another set of credentials to log in to this script, all this is handled by Joomla configuration)
I did everything I could, but when I run the script, I get the following error:
Database Error: Unable to connect to the database:Could not connect to MySQL
I printed the variable and made sure that the connection data for mysql is correct (as it is).
My current code is:
<?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/..' ));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once( JPATH_CONFIGURATION .DS.'configuration.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'database.php' );
require_once ( JPATH_LIBRARIES .DS.'joomla'.DS.'import.php' );
$Config = new JConfig();
$option['driver'] = $Config->dbtype;
$option['host'] = $Config->host;
$option['user'] = $Config->user;
$option['password'] = $Config->password;
$option['database'] = $Config->db;
$option['prefix'] = $Config->dbprefix;
$db = & JDatabase::getInstance($option);
$database =& JFactory::getDBO();
$query = "SELECT * FROM #__chronoforms_RD_NonDangerousGoods WHERE cf_id = 4;";
$database->setQuery($query);
$result = $database->query();
print_r($result);
?>