gpt4 book ai didi

php - 使用框架将独立脚本连接到 joomla DB

转载 作者:可可西里 更新时间:2023-11-01 00:11:23 26 4
gpt4 key购买 nike

我目前正在编写一个脚本,它将作为一个 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);
?>

最佳答案

试试这个

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

关于php - 使用框架将独立脚本连接到 joomla DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4291233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com