title: Connect standalone script to joomla DB using framework
body: <p>Im currently writing a script which will be run as a cronjob to do some calculations using values out of the joomla database, Because this script is not going to be accessed via joomla as a plugin etc i need to do DB connections with it.</p>
  
  <p>What im attempting to do is use the Joomla framework to do all the work(Connection, queries, etc) for security and also portability purposes (instead of having another set of the login credentials in this script its all handled by the Joomla config)</p>
  
  <p>I have done the best i can but when i run the script i get the following error:</p>
  
  <pre><code>Database Error: Unable to connect to the database:Could not connect to MySQL
  </code></pre>
  
  <p>I have printed out the variable and made sure that the connection details for mysql are correct (which they are).</p>
  
  <p>My Current Code is:</p>
  
  <pre><code>&lt;?php
  //init Joomla Framework
  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' );
  
  //DB Connection
  $Config = new JConfig();
  
  $option['driver']   = $Config-&gt;dbtype;   // Database driver name
  $option['host']     = $Config-&gt;host;     // Database host name
  $option['user']     = $Config-&gt;user;     // User for database authentication
  $option['password'] = $Config-&gt;password; // Password for database authentication
  $option['database'] = $Config-&gt;db;       // Database name
  $option['prefix']   = $Config-&gt;dbprefix; // Database prefix (may be empty)
  
  $db = &amp; JDatabase::getInstance($option);
  
  //DBQuery
  $database =&amp; JFactory::getDBO();
  $query = "SELECT * FROM #__chronoforms_RD_NonDangerousGoods WHERE cf_id = 4;";
  $database-&gt;setQuery($query);
  $result = $database-&gt;query();
  print_r($result);
  ?&gt;
  </code></pre>