0

我已经尝试过了,它正在工作:

<?php
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', dirname(__FILE__).DS."../apitest/");

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

$conn = JDatabase::getConnectors();

print_r($conn);
?>

但是,当我尝试这样做时:

<?php
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', dirname(__FILE__).DS."../apitest/");

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

$conn = JDatabase::$connection;

print_r($conn);
?>

它返回:

Fatal error: Cannot access protected property JDatabase::$connection in C:\xampp\htdocs\apitest1\index.php on line 10

如何访问 $connection 变量?

4

2 回答 2

1

试试这个

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
define( 'DS', DIRECTORY_SEPARATOR );

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

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
$db = &JFactory::getDBO(); //Your database object is ready
$sql = "SELECT * FROM #__users";
$db->setQuery($sql);
$db->query();
$res = $db->loadAssocList();
print_r($res)

希望这可以帮助你..

于 2013-02-20T07:43:25.267 回答
0

这是受保护的,因此您需要使用 getter 来读取,并使用 setter 来写入此属性。

谷歌搜索 Joomla API 显示如下:

http://docs.joomla.org/API16:JDatabase/getConnection

示例用法

$conn = $connectorInstance.getConnection();

您可能还想看看这篇文章

于 2013-02-20T07:24:11.123 回答