简体   繁体   中英

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

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

Made a few tweaks to your database query and replaced $_GET with the correct Joomla method

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

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

But if it is a different databas you'll need to supply the connection data in some other way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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