问题描述
我目前正在编写一个脚本,该脚本将作为 cronjob 运行,以使用 joomla 数据库中的值进行一些计算,因为该脚本不会通过 joomla 作为插件等访问,我需要与它进行数据库连接.
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.
我试图做的是使用 Joomla 框架来完成所有工作(连接、查询等),以实现安全性和可移植性(而不是在此脚本中使用另一组登录凭据,所有这些都由 Joomla 处理)配置)
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
我已经打印出变量并确保 mysql 的连接详细信息是正确的(它们是正确的).
I have printed out the variable and made sure that the connection details for mysql are correct (which they are).
我当前的代码是:
<?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);
?>
推荐答案
试试这个
<?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);
?>
这篇关于使用框架将独立脚本连接到 joomla DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)