Connect standalone script to joomla DB using frame

2019-04-12 12:47发布

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.

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)

I have done the best i can 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 have printed out the variable and made sure that the connection details for mysql are correct (which they are).

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);
?>

4条回答
淡お忘
2楼-- · 2019-04-12 13:08

try this

   <?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);
    ?>
查看更多
够拽才男人
3楼-- · 2019-04-12 13:08

To fix "query" deprecation warning use JDatabaseDriver instead JDatabase:

$query = "SELECT * FROM #__chronoforms_RD_NonDangerousGoods WHERE cf_id = 4;";       
$database =& JFactory::getDbo();
$database->setQuery($query);
$result = $database->execute();
print_r($result)
查看更多
倾城 Initia
4楼-- · 2019-04-12 13:09

This works for Joomla 2.5 (and 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: The goal here is to connect to database using already set up Joomla configuration and execute SQL queries with Joomla's DBO. This way we can do things like inserting stuff into tables without manually entering credentials in our script.

查看更多
唯我独甜
5楼-- · 2019-04-12 13:12

here is the joomla bootstrap code for joomla 3.x. Make sure your JOOMLA_PATH is correct, it should be the path of your Joomla installation

//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';
查看更多
登录 后发表回答