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");
}
}
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.
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 Please make that private instead of public.
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.
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.
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.
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.
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 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.