Difference between revisions of "Connecting to an external database"

From Joomla! Documentation

(Importing text file)
 
m (needs review)
(16 intermediate revisions by 8 users not shown)
Line 1: Line 1:
======How to connect to an external database======
+
{{needs|needs review|The functions used here are out of date and need to be updated}}
 +
If you need to access tables within the same database as your Joomla! installation then you can simply use the [[JFactory/getDBO|JFactory->getDbo]] method.  This uses the already established connection that Joomla! uses to connect to the database.  For example:
  
If you need to access tables within the same database as your Joomla! installation then you can simply use the [[references:joomla.framework:jfactory-getdbo|JFactory::getDBO]] method.  This uses the already established connection that Joomla! uses to connect to the database.  For example:
+
<source lang="php">
 
 
<source>
 
 
<?php
 
<?php
$db = JFactory::getDBO();
+
$db = JFactory::getDbo();
 
?>
 
?>
 
</source>
 
</source>
  
$db is now an object of type [[references:joomla.framework:database:jdatabase|JDatabase]] and you can perform database operations on it using the usual methods.
+
$db is now an object of type [http://api.joomla.org/Joomla-Platform/Database/JDatabase.html JDatabase] and you can perform database operations on it using the usual methods.
  
But what if you want to connect to a completely different database from the one used by Joomla!?.  This might be a different database on the same machine as your Joomla! site or it might be on a different host and perhaps even require a different database driver.  Well, you can do this using the [[references:joomla.framework:database:jdatabase-getinstance|JDatabase::getInstance]] method.
+
But what if you want to connect to a completely different database from the one used by Joomla!?.  This might be a different database on the same machine as your Joomla! site or it might be on a different host and perhaps even require a different database driver.  Well, you can do this using the [http://api.joomla.org/Joomla-Platform/Database/JDatabase.html#getInstance JDatabase->getInstance] method.
  
<source>
+
<source lang="php">
 
<?php
 
<?php
 +
$option = array(); //prevent problems
 +
 
$option['driver']  = 'mysql';            // Database driver name
 
$option['driver']  = 'mysql';            // Database driver name
 
$option['host']    = 'db.myhost.com';    // Database host name
 
$option['host']    = 'db.myhost.com';    // Database host name
 
$option['user']    = 'fredbloggs';      // User for database authentication
 
$option['user']    = 'fredbloggs';      // User for database authentication
$option['password'] = 's9(39s£h[%dkFd'; // Password for database authentication
+
$option['password'] = 's9(39s£h[%dkFd';   // Password for database authentication
 
$option['database'] = 'bigdatabase';      // Database name
 
$option['database'] = 'bigdatabase';      // Database name
 
$option['prefix']  = 'abc_';            // Database prefix (may be empty)
 
$option['prefix']  = 'abc_';            // Database prefix (may be empty)
  
$db = & JDatabase::getInstance( $option );
+
$db = JDatabase::getInstance( $option );
 
?>
 
?>
 
</source>
 
</source>
  
$db is now an object of type [[references:joomla.framework:database:jdatabase|JDatabase]] and you can perform database operations on it using the usual methods.
+
$db is now an object of type [http://api.joomla.org/Joomla-Platform/Database/JDatabase.html JDatabase] and you can perform database operations on it using the usual methods.
  
 
Note that if the database uses a non-standard port number then this can be specified by adding it to the end of the host name.  For example, you might have your MySQL database running on port 3307 (the default is port 3306), in which case your host name might be 'db.myhost.com:3307'.
 
Note that if the database uses a non-standard port number then this can be specified by adding it to the end of the host name.  For example, you might have your MySQL database running on port 3307 (the default is port 3306), in which case your host name might be 'db.myhost.com:3307'.
  
One feature of using [[references:joomla.framework:database:jdatabase-getinstance|JDatabase::getInstance]] is that if another call is made with the same parameters it will return the previously created object rather than creating a fresh one.
+
One feature of using [http://api.joomla.org/Joomla-Platform/Database/JDatabase.html#getInstance JDatabase->getInstance] is that if another call is made with the same parameters it will return the previously created object rather than creating a fresh one.
 +
 
 +
Note, however, that the parameters must match exactly for this to happen.  For example, if two calls were made to a MySQL database using [http://api.joomla.org/Joomla-Platform/Database/JDatabase.html#getInstance JDatabase->getInstance], with the first using a host name of 'db.myhost.com' and the second using 'db.myhost.com:3306', then two separate connections would be made, even though port 3306 is the default port for MySQL and so the parameters are logically the same.
 +
 
 +
If you want to use the JModel with pagination etc. you can choose a different way. The point is, you have to replace the standard database object linked to the Joomla background database using [[JDatabase/setDbo|JDatabase->setDbo]]. The first step is to override the constructor of your JModel in your model file
 +
<source>
 +
public function __construct($config = array())
 +
{
 +
parent::__construct($config);
  
Note, however, that the parameters must match exactly for this to happen.  For example, if two calls were made to a MySQL database using [[references:joomla.framework:database:jdatabase-getinstance|JDatabase::getInstance]], with the first using a host name of 'db.myhost.com' and the second using 'db.myhost.com:3306', then two separate connections would be made, even though port 3306 is the default port for MySQL and so the parameters are logically the same.
+
$option = array(); //prevent problems
 +
 +
$option['driver']  = 'mysql';            // Database driver name
 +
$option['host']    = 'localhost';    // Database host name
 +
$option['user']    = 'myusername';      // User for database authentication
 +
$option['password'] = 'saltedpassword';  // Password for database authentication
 +
$option['database'] = 'db_extern';      // Database name
 +
$option['prefix']  = '';            // Database prefix (may be empty)
 +
 +
$db = JDatabase::getInstance( $option );
 +
parent::setDbo($db);
 +
}
 +
</source>
 +
After that JModel behaves normal but uses your database.
 +
<noinclude>[[Category:Development]][[Category:Database]][[Category:FAQ]][[Category:Tutorials]]</noinclude>

Revision as of 18:16, 29 September 2013

Copyedit.png
This Article Needs Your Help

This article is tagged because it NEEDS REVIEW. You can help the Joomla! Documentation Wiki by contributing to it.
More pages that need help similar to this one are here. NOTE-If you feel the need is satistified, please remove this notice.

Reason: The functions used here are out of date and need to be updated


If you need to access tables within the same database as your Joomla! installation then you can simply use the JFactory->getDbo method. This uses the already established connection that Joomla! uses to connect to the database. For example:

<?php
$db = JFactory::getDbo();
?>

$db is now an object of type JDatabase and you can perform database operations on it using the usual methods.

But what if you want to connect to a completely different database from the one used by Joomla!?. This might be a different database on the same machine as your Joomla! site or it might be on a different host and perhaps even require a different database driver. Well, you can do this using the JDatabase->getInstance method.

<?php
$option = array(); //prevent problems

$option['driver']   = 'mysql';            // Database driver name
$option['host']     = 'db.myhost.com';    // Database host name
$option['user']     = 'fredbloggs';       // User for database authentication
$option['password'] = 's9(39s£h[%dkFd';   // Password for database authentication
$option['database'] = 'bigdatabase';      // Database name
$option['prefix']   = 'abc_';             // Database prefix (may be empty)

$db = JDatabase::getInstance( $option );
?>

$db is now an object of type JDatabase and you can perform database operations on it using the usual methods.

Note that if the database uses a non-standard port number then this can be specified by adding it to the end of the host name. For example, you might have your MySQL database running on port 3307 (the default is port 3306), in which case your host name might be 'db.myhost.com:3307'.

One feature of using JDatabase->getInstance is that if another call is made with the same parameters it will return the previously created object rather than creating a fresh one.

Note, however, that the parameters must match exactly for this to happen. For example, if two calls were made to a MySQL database using JDatabase->getInstance, with the first using a host name of 'db.myhost.com' and the second using 'db.myhost.com:3306', then two separate connections would be made, even though port 3306 is the default port for MySQL and so the parameters are logically the same.

If you want to use the JModel with pagination etc. you can choose a different way. The point is, you have to replace the standard database object linked to the Joomla background database using JDatabase->setDbo. The first step is to override the constructor of your JModel in your model file

public function __construct($config = array())
	 {
		parent::__construct($config);

		$option = array(); //prevent problems
 
		$option['driver']   = 'mysql';            // Database driver name
		$option['host']     = 'localhost';    // Database host name
		$option['user']     = 'myusername';       // User for database authentication
		$option['password'] = 'saltedpassword';   // Password for database authentication
		$option['database'] = 'db_extern';      // Database name
		$option['prefix']   = '';             // Database prefix (may be empty)
		
		$db = JDatabase::getInstance( $option );
		parent::setDbo($db);
	 }

After that JModel behaves normal but uses your database.