Synchronize data between 2 sites dynamically
You need to work on the database a little bit.

I think it is impossible to make it work if you just write php. Because it will generate a lot of database login errors. 

There might be a lot of ways to do that, but the only way I know is to:
First, Combine two database into one, two different databases with two different prefix.
Second, delete all the attributes you want to synchronize in the second database.
Third, link the synchronized field in the second database to the first database, you can do this in the settings.php in the second drupal site.

But there might be some dependency problems when you do step 2. Don't forget to backup at first.

This link might be helpful How to connect to multiple databases within Drupal