Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Sign In
Not a member yet? Click here to register.
Cupid

using multiple databases

by Cupid, Last updated on 2 months ago in Modifications and Requests - 8
So I'm currently running v7 since I'm having problems upgrading to v8.

Anyway. I'm sure the answer will be similar.

How would I have php-fusion connect to two databases at the same time?

I want to have a completely different site but using the users from a different database. Using multisite_includes if possible.

Thank you for your advice!
Essentially start with a new connection within your Fusion site and trigger it then reconnect to current with default config.
If you do it on 8 you will need a db_driver var aswell. see config.php for full syntaxes.

But this is the basics of it all.

<?php
$server = "localhost"; // New Server
$username = ""; // Username
$password = ""; // Password
$database_name ="NEW_DATABASE"; // New Database

// Actions, includes etc.


// Reconnect to Fusion database, do any new includes required first.
dbconnect($db_host, $db_user, $db_pass, $db_name);
Need help?, Having trouble?
• View our Documentation for Guides, Standards and Functions
• Name and Organize your content correctly in the corresponding Forums for best support results
• Attaching Log Files and Screenshots when reporting issues will help
• Please read and comply with the Code of Conduct

(¯`·._.·(¯`°·._.·°º*[ Project Manager ]*º°·._.·°´¯)·._.·´¯)
I see. So would that code have to go into the maincore.php or be a separate file?

Forgive me I don't know much about php but I am watching some videos and trying to learn.

How does it know to grab information from 2 databases?
You could make 2 config files and include a new one for the app / file you want to switch database in the opening, like after maincore.php add include config_2.php then run a connect sequence on it , dbconnect($db_host, $db_user, $db_pass, $db_name);

at the end of the script include config.php and reconnect.
Need help?, Having trouble?
• View our Documentation for Guides, Standards and Functions
• Name and Organize your content correctly in the corresponding Forums for best support results
• Attaching Log Files and Screenshots when reporting issues will help
• Please read and comply with the Code of Conduct

(¯`·._.·(¯`°·._.·°º*[ Project Manager ]*º°·._.·°´¯)·._.·´¯)
cupid.vivalavoltz.com/images/linksonsites/iam5.jpg

Sorry like I said. I'm not very good at PHP yet. If you don't mind. I think I get the basics of what you're trying to say.
The main thing is You van use only one database connection at a time.
It sounds like you want to have two completely separate sites but share the same user database? That's almost exactly what multi-site is, and I run two independent PHP-Fusion sites except they share the same user database. To do this I followed the steps that are in the PHP-Fusion v7 manual. You may want to see if this suits your requirements?
Cupid has replied to this - 2 months ago
That's not what I want. I know of multisite and I currently use it.

I want to have a completely separate site on a completely separate database but use the users from a different database. So it would have to pull information from different databases.
It is not possible without doing heavy modifications to the Core, unless you have very few areas where it should be applicable and can switch database as per description in them few areas.
Need help?, Having trouble?
• View our Documentation for Guides, Standards and Functions
• Name and Organize your content correctly in the corresponding Forums for best support results
• Attaching Log Files and Screenshots when reporting issues will help
• Please read and comply with the Code of Conduct

(¯`·._.·(¯`°·._.·°º*[ Project Manager ]*º°·._.·°´¯)·._.·´¯)
Maybe you can setup master slave replication between the two databases, so the data from the remote database is also available in the local database as a copy
Cupid has replied to this - 2 months ago
If I am reading this right, you want to make a second site but use back the old user pool you have in the first site? I want to confirm on this before I provide my answer.
Lead Developer of PHP-Fusion
Developer Tweet: https://twitter.com/phpfusion_tweet

Chan wrote:

@Chan - If I am reading this right, you want to make a second site but use back the old user pool you have in the first site? I want to confirm on this before I provide my answer.


Yes you are correct. But I don't want to just use multi site. I want to keep the two sites on separate databases. I want to do this for organization purposes.

Merged on 04/18/2019:

douwe_yntema wrote:

@douwe_yntema - Maybe you can setup master slave replication between the two databases, so the data from the remote database is also available in the local database as a copy


That's certainly a good idea. I never knew that existed. I wonder how hard it is.
@cupid

Okay, then you can use the dbconnect() and then tie it to your 2nd's site DB_USERS.

In your 2nd site, see this line https://github.com/php-fusion/PHP-Fus...ss.php#L51

Before Line 51.

$db_host = 'localhost';
$db_user = 'your-site1-db-user';
$db_pass = 'your-site1-db-pass';
$db_name = 'your-site1-dbname';
$db_port = 3306;
dbconnect($db_host, $db_user, $db_pass, $db_name, !empty($db_port) ? $db_port : 3306);
unset($db_host, $db_user, $db_pass, $db_port);


Then, you need to reconnect to back to the previous connection here:
https://github.com/php-fusion/PHP-Fus...e.php#L207

include BASEDIR.'config.php';
$link = @dbconnect($db_host, $db_user, $db_pass, $db_name, !empty($db_port) ? $db_port : 3306);
unset($db_host, $db_user, $db_pass, $db_port);


Now compare both site's config.php file, make sure the site2 one is same as site1

define("COOKIE_PREFIX", "loremipsum_");


This will give your user same persistent login and when they visit the second site, they are continued to be login state.

Multisite is just is not as it seems. Don't be misguided :) That file should read 'constants.php'

To have "multisite" you need - "multi" connections, but as everyone had mentioned here, you can only have 1 active connection. So you need to know where to close a connection and open a new connection line by the process workflow.

Caveat: I have not tested any of this. There are 2 system version 8 and version 9 to look into. I'm just typing this from the back of my head. These are just pointers to get you going. I don't have much room for another side lined test case. I have already 40+ open tabs on my IDE now. I can't lose my focus at this moment :)
Lead Developer of PHP-Fusion
Developer Tweet: https://twitter.com/phpfusion_tweet
Thank you for your response! I think I'm going to focus more on getting my sites up to date first and then worry about this.

It's not necessary right now since I could just use multi site.
You can view all discussion threads in this forum.
You can start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You can download attachments in this forum.
Users who participated in discussion: songiuno, Falk, Chan, Cupid, douwe_yntema