Connect stand-alone script to joomla DB using framework

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
//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->dbtype;   // Database driver name
$option['host']     = $Config->host;     // Database host name
$option['user']     = $Config->user;     // User for database authentication
$option['password'] = $Config->password; // Password for database authentication
$option['database'] = $Config->db;       // Database name
$option['prefix']   = $Config->dbprefix; // Database prefix (may be empty)

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

//DBQuery
$database =& JFactory::getDBO();
$query = "SELECT * FROM #__chronoforms_RD_NonDangerousGoods WHERE cf_id = 4;";
$database->setQuery($query);
$result = $database->query();
print_r($result);
?>
+3
3

   <?php
        //init Joomla Framework
        define( '_JEXEC', 1 );
        define( 'DS', DIRECTORY_SEPARATOR );
        define( 'JPATH_BASE', realpath(dirname(__FILE__).DS.'..' ));


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

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

        //DBQuery
        $database =& JFactory::getDBO();
        $query = "SELECT * FROM #__chronoforms_RD_NonDangerousGoods WHERE cf_id = 4;";
        $database->setQuery($query);
        $result = $database->query();
        print_r($result);
    ?>
+6

Joomla 2.5 ( 3.5)

    define( '_JEXEC', 1); //  This will allow to access file outside of joomla.
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', realpath(dirname(__FILE__) .'/' ) );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$user =JFactory::getUser();
$session =& JFactory::getSession();
$database = JFactory::getDBO();

EDIT: Joomla SQL- Joomla DBO. , , script.

+2

joomla joomla 3.x. , JOOMLA_PATH , Joomla.

//joomla bootstrap
define( 'DS', DIRECTORY_SEPARATOR );
error_reporting(E_ALL);
date_default_timezone_set('UTC');
define('_JEXEC', 1);
define('JOOMLA_PATH',realpath(dirname(__FILE__).DS.'..'.DS.'..' ));
$_SERVER['HTTP_HOST'] = 'localhost';
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '';

if (file_exists(JOOMLA_PATH . '/defines.php'))
{
    include_once JOOMLA_PATH . '/defines.php';
}

if (!defined('_JDEFINES'))
{
    define('JPATH_BASE', JOOMLA_PATH);
    require_once JPATH_BASE . '/includes/defines.php';
}

require_once JPATH_BASE . '/includes/framework.php';
+1

Source: https://habr.com/ru/post/1777169/


All Articles