Joomla provides two methods to access database:
1. The below method is used to access the Joomla database.
JFactory->getDBO() (Internal database access)
2. This method is used to access the database other than joomla database i.e, an external database. This might be happen sometimes that you require a totally different database in your project.
You can do this by using the method as shown below-
JDatabase->getInstance()
Ex:
$external_db = array(); //prevent problems
$external_db['driver'] = 'mysql'; // Database driver name
$external_db['host'] = 'localhost'; // Database host name
$external_db['user'] = 'root'; // User for database authentication
$external_db['password'] = 'root'; // Password for database authentication
$external_db['database'] = 'databasename'; // Database name
$db = & JDatabase::getInstance( $external_db );
Here, $db is an object of JDatabaseDriver type. Database operations such as insert, update, delete etc., can be done by using the usual methods of joomla.
Ex:
$external_db = array();
$external_db['driver'] = 'mysql'; // Database driver name
$external_db['host'] = 'localhost'; // Database host name
$external_db['user'] = 'root'; // User for database authentication
$external_db['password'] = 'root'; // Password for database authentication
$external_db['database'] = 'databasename'; // Database name
$db = & JDatabase::getInstance( $external_db );
$sql="select query"; // SQL
$db->setQuery ( $sql );
$db->query ();
$res = $db->loadAssocList ();
0 Comment(s)