How to connect 5 databases on the same host

Hello, as title says i need to connect to 5 (MyDatabase1, MyDatabase2, MyDatabase3, MyDatabase4, MyDatabase5) different databases to get back information. I need these connections only in one page of my website.
If it would be pure mysqli i could do it but i m not too sure about this class:

class engine_base {
   public $con;

   public function __construct() {
	define("HOST", "MyHost");
	define("USER", "MyUser");
	define("PASSWORD", "MyPassword");
	define("DB", "MyDatabase1");
	$this->con = mysqli_connect(HOST,USER,PASSWORD,DB);
	mysqli_set_charset( $this->con, 'utf8');
	ini_set("date.timezone", "Europe/Helsinki");
	}
}

How to do it? And is it possible?

Store the connection information in a constant array and connect to each of them.

i do not get it :frowning:

  1. What is the ini_set doing in that class? It’s an application concern, that would be best solved in your php.ini, and if you can’t put it there, but it in your application bootstrap, like an index.php.

  2. Why is the $con property public? The whole point of classes is encapsulation, but this class doesn’t encapsulate anything, as the main resource it’s supposed to protect is wide open :eek: Please make that private instead of public.

  3. To support multiple databases you need to apply inversion of control; instead of defining the credentials in the class itself, define them outside the class and pass them in as parameters. Something like this:

<?php

class engine_base {
    private $con;
    public function __construct($hostname, $username, $password, $database, $charset = 'utf8')
    {
        $this->con = mysqli_connect($hostname, $username, $password, $database);
        mysqli_set_charset($this->con, $charset);
    }
}

And then to create the instances:

$db1 = new engine_base('my-hostname', 'my-username', 'my-password', 'database1');
$db2 = new engine_base('my-hostname', 'my-username', 'my-password', 'database2');
$db3 = new engine_base('my-hostname', 'my-username', 'my-password', 'database3');
$db4 = new engine_base('my-hostname', 'my-username', 'my-password', 'database4');
$db5 = new engine_base('my-hostname', 'my-username', 'my-password', 'database5');

Giving them descriptive names (like for example $userDatabase, $salesDatabase, etc) would be easier to manage though.

Hmm ok will try it! But few thinks that are confusing me is:

  1. How to not affect exiting connection?

  2. Should i put these lines

on same file as engine base is and should it be under class engine_base { ... } ??

Can you post the code where engine_base is currently created?

do you think this?

require_once("engine_base.php");

$co = new engine_base();

Well if you use the values that are now defined in your class and pass them as arguments instead you have exactly the same connection.

Of course you should also call it $co then instead of $db1.

Thanks for help but i still confused what i need to do :smiley:

I can’t make it any more clear. Just play with it a bit and see if you can make it work. If you can’t you should probably take a step back and start learning some PHP basics first.

1 Like
<?php

class engine_base {
   
   private $hosts = [ // store hosts
	'db1' => [
		'host' => '',
		'user' => '',
		'pass' => '',
		'db' => '',
	],
   ];
   
   private connections = []; // store multiple connections
   
   private function connectAll(){ // connect to all hosts
       foreach($this->hosts as $alias => $host){
           $this->connections[$alias] = mysqli_connect($host['host'], ...);
       }
   }
   
   public function getConnection($alias){ // receive a defined connection
       if(!empty($this->connections[$alias])) return ...
   }
    
   public $con;

   public function __construct() {
	define("HOST", "MyHost");
	define("USER", "MyUser");
	define("PASSWORD", "MyPassword");
	define("DB", "MyDatabase1");
	$this->con = mysqli_connect(HOST,USER,PASSWORD,DB);
	mysqli_set_charset( $this->con, 'utf8');
	ini_set("date.timezone", "Europe/Helsinki");
	}
}
```

No one has asked the obvious, why are you using five separate databases? This sounds like a bigger problem at hand. I smell an XY problem.

I have data in each of databases that i want to add and show to user in one single html table

That part was already clear. You still have not answered the question as to why the data is in five separate databases if it is all related enough to display in one place at the same time. Describe what you have going on with the data.

They are 5 seperate websites :wink:

Code like that gets complicated really really fast, because now you have a single class that knows about how to connect to a single database, but also how to connect to multiple databases.

It would be a lot better to keep engine_base as is, and add some sort of registry to keep track of multiple connections.

like make new class for every other connection?

Assuming that the actual task should be getting data from five databases into one website, you need to use dependency injection and a single class. I am still interested to know exactly what you have going on that you are pulling 5 websites data into one place.

In each of sites i have userbase and i want to show specific actions in html table for each websites for specific users (based on users emails)

And the Mystery deepens…

In any case, you need a single connection class that uses dependency injection to get your multiple connections. Very simple to do. If you do not already know about Dependency Injection now is the time to read up about it and learn it. If you get stuck let me know.