简体   繁体   English

如何从外部php文件连接joomla3 DB?

[英]How to connect joomla3 DB from external php file?

I have created external php file inside my module and there i have use some sql queries , first i have tried using php/mysql and it works , then i tried to make it convert to joomla style . 我已经在我的模块中创建了外部php文件,并且我已经使用了一些sql查询,首先我尝试使用php / mysql并且它有效,然后我尝试将其转换为joomla样式。 but when i use joomla framework to db connection gives errors 但是当我使用joomla框架进行数据库连接时会出错

Old code: FROM PHP 旧代码:FROM PHP

mysql_connect("localhost","root","");
mysql_select_db("1234");

 $searchp=$_GET["term"];
 $query=mysql_query("SELECT * FROM sltdb_cddir_content where title like '%".$searchp."%'AND categories_id=82 order by title ASC ");
 $json=array();
    while($display=mysql_fetch_array($query)){
         $json[]=array(
                    'value'=> $display["title"],
                    'label'=>$display["title"]
                        );
    }

 echo json_encode($json);

New Code : JOOMLA3 新规范:JOOMLA3

    define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT'] . DS . '');

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

//create application
$mainframe = &JFactory::getApplication('site');
$db = JFactory::getDBO();

// Create a new query object.
$query = $db -> getQuery(true);
$searchp = $_GET["term"];
$query -> select($db -> quoteName(array('title')));
$query -> from($db -> quoteName('sltdb_cddir_content'));
$query -> where($db -> quoteName('title') . ' LIKE ' . $db -> quote('\'$searchp.%\''));
$query -> order('ordering ASC');

$db -> setQuery($query);
$json = array();
while ($display = mysql_fetch_array($query)) {
    $json[] = array('value' => $display["title"], 'label' => $display["title"]);
}

echo json_encode($json);

once after converting to the code in joomla its given a error 转换为joomla中的代码后,它给出了一个错误

*"mysql_fetch_array() expects parameter 1 to be resource, object given in "* *“mysql_fetch_array()期望参数1是资源,对象在”*中给出

Please advice me where i have done incorrect . 请告诉我我做错了什么。

EDIT 01 编辑01

    define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT'] . DS . '');

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

//create application
$mainframe = &JFactory::getApplication('site');
$searchp = $_GET["term"];
$db = JFactory::getDBO();

// Create a new query object.
$query = $db -> getQuery(true);

$query -> select($db -> quoteName(array('title')));
$query -> from($db -> quoteName('sltdb_cddir_content'));
$query -> where($db -> quoteName('title') . ' LIKE ' . $db -> quote('\'$searchp.%\''));
$query->where($db->quoteName('categories_id')." = ".$db->quote(82));
$query -> order('ordering ASC');

$db->setQuery($query);

$results = $db-> loadAssocList();
$json = array();
foreach($results as $json_result) {
  $json[] = array('value' => $json_result["title"], 'label' => $json_result["title"]) ;
}

echo json_encode($json);

You are mixing using Joomla's "SDK" with pure PHP methods for data access, so I would recommend stay away from that mix: 你正在使用Joomla的“SDK”与纯PHP方法混合进行数据访问,所以我建议远离这种混合:

1) mysql_fetch_array its going to be depreacated, and indeed it expects the 1st parameter to be a mysql connection created with a mysql_connect call, that's why the error says that the 1st parameter expects to be a resource, you can check the documentation in here http://www.php.net/mysql_fetch_array . 1)mysql_fetch_array它将被删除,实际上它期望第一个参数是用mysql_connect调用创建的mysql连接,这就是为什么错误说第一个参数应该是一个资源,你可以在这里查看文档http ://www.php.net/mysql_fetch_array

2) Since you are using Data Access classes from joomla to obtain the connection and build the query, which is fine, I would recommend keep using it for obtaining the results and looping through them, like this. 2)由于您使用joomla的数据访问类来获取连接并构建查询,这很好,我建议继续使用它来获取结果并循环遍历它们,就像这样。

// Reset the query using our newly populated query object.
$db->setQuery($query);

// Load the results as a list of associated arrays.
$results = $db-> loadAssocList();
$json = array();
foreach($results as $json_result) {
  $json[] = array('value' => $json_result["title"], 'label' => $json_result["title"]) 
}

More about Joomla's DB access here: http://docs.joomla.org/Selecting_data_using_JDatabase 有关Joomla数据库访问的更多信息,请访问: http//docs.joomla.org/Selecting_data_using_JDatabase

Made a few tweaks to your database query and replaced $_GET with the correct Joomla method 对您的数据库查询进行了一些调整,并使用正确的Joomla方法替换了$_GET

define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT'] . DS . '');
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');

$app    = JFactory::getApplication();
$jinput = $app->input;
$db     = JFactory::getDbo();

$searchp = $jinput->get('term', null, null);
$id = 82;

$query = $db->getQuery(true);
$query->select($db->quoteName('title'))
      ->from($db->quoteName('sltdb_cddir_content'))
      ->where($db->quoteName('title') . ' LIKE ' . $db->quote($searchp))
      ->where($db->quoteName('categories_id') . ' = ' . $db->quote((int) $id))
      ->order('ordering ASC');
$db->setQuery($query);

Let me know if it works 如果有效,请告诉我

  1. Use 采用

    echo $query->dump(); echo $ query-> dump();

To see what the generated query looks like and you can then test the query directly. 要查看生成的查询的外观,您可以直接测试查询。

  1. You need to connect to the database ... is it the same or different? 您需要连接到数据库......它是相同还是不同? If it is the same you can do 如果它是相同的你可以做

    // Creating the database connection. //创建数据库连接。 $this->db = JDatabase::getInstance( array( 'driver' => $config->dbtype, 'host' => $config->host, 'user' => $config->user, 'password' => $config->password, 'database' => $config->db, 'prefix' => $config->dbprefix, ) ); $ this-> db = JDatabase :: getInstance(array('driver'=> $ config-> dbtype,'host'=> $ config-> host,'user'=> $ config-> user,'password'= > $ config-> password,'database'=> $ config-> db,'prefix'=> $ config-> dbprefix,));

But if it is a different databas you'll need to supply the connection data in some other way. 但如果它是不同的数据库,则需要以其他方式提供连接数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM