我正在编写一个脚本,它将作为cronjob运行,使用joomla数据库中的值进行一些计算,因为这个脚本不会通过joomla作为插件等访问,我需要与它进行数据库连接
我试图做的是使用Joomla框架来完成所有工作(连接,查询等)以实现安全性和可移植性目的(而不是在此脚本中使用另一组登录凭据全部由Joomla处理配置)
我已经做到了最好,但是当我运行脚本时,我收到以下错误:
Database Error: Unable to connect to the database:Could not connect to MySQL
我打印出变量并确保mysql的连接细节是正确的(它们是)。
我的当前代码是:
<?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);
?>
答案 0 :(得分:6)
试试这个
<?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);
?>
答案 1 :(得分:2)
这适用于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();
编辑:这里的目标是使用已经设置的Joomla配置连接到数据库,并使用Joomla的DBO执行SQL查询。通过这种方式,我们可以执行诸如将内容插入表中而无需在脚本中手动输入凭据的操作。
答案 2 :(得分:1)
这是joomla 3.x的joomla bootstrap代码。确保你的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';
答案 3 :(得分:0)
要解决“查询”弃用警告,请使用JDatabaseDriver
代替JDatabase
:
$query = "SELECT * FROM #__chronoforms_RD_NonDangerousGoods WHERE cf_id = 4;";
$database =& JFactory::getDbo();
$database->setQuery($query);
$result = $database->execute();
print_r($result)