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