dth981485742 2014-03-11 17:26
浏览 21
已采纳

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

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 . but when i use joomla framework to db connection gives errors

Old code: 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

    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

*"mysql_fetch_array() expects parameter 1 to be resource, object given in "*

Please advice me where i have done incorrect .

EDIT 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);
  • 写回答

3条回答 默认 最新

  • dongshu4221 2014-03-11 17:56
    关注

    You are mixing using Joomla's "SDK" with pure PHP methods for data access, so I would recommend stay away from that mix:

    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.

    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.

    // 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)