title: How to connect joomla3 DB from external php file?
body: <p>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 </p>
  
  <p><strong>Old code: FROM PHP</strong> </p>
  
  <pre><code>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'=&gt; $display["title"],
                      'label'=&gt;$display["title"]
                          );
      }
  
   echo json_encode($json);
  </code></pre>
  
  <p><strong>New Code : JOOMLA3</strong> </p>
  
  <pre><code>    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 = &amp;JFactory::getApplication('site');
  $db = JFactory::getDBO();
  
  // Create a new query object.
  $query = $db -&gt; getQuery(true);
  $searchp = $_GET["term"];
  $query -&gt; select($db -&gt; quoteName(array('title')));
  $query -&gt; from($db -&gt; quoteName('sltdb_cddir_content'));
  $query -&gt; where($db -&gt; quoteName('title') . ' LIKE ' . $db -&gt; quote('\'$searchp.%\''));
  $query -&gt; order('ordering ASC');
  
  $db -&gt; setQuery($query);
  $json = array();
  while ($display = mysql_fetch_array($query)) {
      $json[] = array('value' =&gt; $display["title"], 'label' =&gt; $display["title"]);
  }
  
  echo json_encode($json);
  </code></pre>
  
  <p>once after converting to the code in joomla its given a error </p>
  
  <p>*"mysql_fetch_array() expects parameter 1 to be resource, object given in "*</p>
  
  <p>Please advice me where i have done incorrect .</p>
  
  <p><strong>EDIT 01</strong></p>
  
  <pre><code>    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 = &amp;JFactory::getApplication('site');
  $searchp = $_GET["term"];
  $db = JFactory::getDBO();
  
  // Create a new query object.
  $query = $db -&gt; getQuery(true);
  
  $query -&gt; select($db -&gt; quoteName(array('title')));
  $query -&gt; from($db -&gt; quoteName('sltdb_cddir_content'));
  $query -&gt; where($db -&gt; quoteName('title') . ' LIKE ' . $db -&gt; quote('\'$searchp.%\''));
  $query-&gt;where($db-&gt;quoteName('categories_id')." = ".$db-&gt;quote(82));
  $query -&gt; order('ordering ASC');
  
  $db-&gt;setQuery($query);
  
  $results = $db-&gt; loadAssocList();
  $json = array();
  foreach($results as $json_result) {
    $json[] = array('value' =&gt; $json_result["title"], 'label' =&gt; $json_result["title"]) ;
  }
  
  echo json_encode($json);
  </code></pre>