3

I have two Joomla sites with different databases. I want to share all data from one site to another site. For example, when I register a user in one website then it's updated on the second site's database too.

I have this code to connect the external database, but I am new to Joomla so don't know how to implement it.

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

$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 = JDatabaseDriver::getInstance( $option );
?>
1
  • Do you want registrations and user profile updates to happen on both sites or only one? Commented Sep 21, 2017 at 17:48

2 Answers 2

2

EDIT 2:

I think this is not the explanation wanted for the user, but it's possible he needs to populate the information of his master website with a plugin system to the two databases.


If you are going to implement the code by using overrides and custom components, you just have to use, for example, this simple guide to obtain information from the other database:

https://docs.joomla.org/Selecting_data_using_JDatabase

EDIT:

More specifically, you have to create two objects, one for the local connection, and another one to the external connection.

$db = JFactory::getDbo();
$extDb = JDatabaseDriver::getInstance( $option );

And use the same query you create for the two objects:

$query = "UPDATE #__anytable SET column1 = value1";
$db->setQuery($query); $db->execute();
$extDb->setQuery($query); $extDb->execute();

PD: Don't post the credentials like user and password on the internet.

8
  • troyKoutei@ thanks for reply, but my query is how to connect two database.see this docs.joomla.org/Connecting_to_an_external_database Commented Sep 21, 2017 at 7:53
  • sanjay-yadav@ Put the external database in another object (in the code above you are overwriting the pointer of the local connection to the external). You only have to use the right object to select, update or delete in each object. For example: If you are going to update an object, use the same query on the two objects.
    – troyKoutei
    Commented Sep 21, 2017 at 8:01
  • troyKoutei@please read the question Commented Sep 21, 2017 at 8:35
  • troyKoutei@@I need to share database , I do not need any query, simply can you let me know How can I connect two database Commented Sep 21, 2017 at 8:37
  • sanjay-yadav@ In that case you have to make a plugin that updates each database with each of the cases you want to update the databases. I mean, you can't open two connections simultaneously in only one object (at least, i don't know if it's possible).
    – troyKoutei
    Commented Sep 21, 2017 at 8:41
2

There is already a simple solution that exists to achieve your aims.

If you install the Fabrik extension you can then add a new database connection within the component admin settings. Using Fabrik you can then display and update fields and data from different databases within the same forms.

6
  • William Ellis@this is commercial extension Commented Sep 21, 2017 at 11:47
  • William Ellis@with this extension its possible to share two database on instant time? Commented Sep 21, 2017 at 11:52
  • William Ellis@query is when a user register in first site then it's data insert and update in both two database Commented Sep 21, 2017 at 11:57
  • The basic component is free. The databases are separate so in reality they couldn't be updated at the exact same time but very close to it! If you read the documentation then you can learn how to update the same field in separate databases. Commented Sep 21, 2017 at 12:00
  • William Ellis@@ I can understand .let me know it's possible to update different database at the same time . in joomla Commented Sep 21, 2017 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.