MyBB Community Forums

Full Version: Connecting to multiple databases on the same server
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone!

This is my first post here and I just found something quite nasty in the code for MyBB that could be very problematic if you're trying to connect to other databases on the same server.

This will only affect you if you're requiring "global.php" in order to integrate things from MyBB with your website.

If you're like me, and have integrated how MyBB works into your site (accounts, forum posts, etc), you may have noticed that you cannot simply select another database on the same server.

This is because MyBB has not provided the new_link parameter in their "db_mysql.php" (or "db_[dbtype].php") file, therefore you can only use one connection. The new_link is a new parameter which was introduced in version 4.2.0, and it forces mysql to create a new link, allowing you to use another connection to a different database on the same server. I'm guessing MyBB may have purposefully left this parameter out to allow compatibility with php versions before 4.2.0
Full info here: http://www.php.net/mysql_connect

However, if you do have version 4.2.0 or later, then I suggest including this parameter so you can connect to other databases.

Simply find inc/db_mysql.php (where "mysql" is the name of you database type) and edit the following lines:

Line 25:
$this->link = @mysql_pconnect($hostname, $username, $password) or $this->dberror();

Line 29:
$this->link = @mysql_connect($hostname, $username, $password) or $this->dberror();

And include the new_link parameter so these two lines now look like this:
$this->link = @mysql_pconnect($hostname, $username, $password, true) or $this->dberror();

$this->link = @mysql_connect($hostname, $username, $password, true) or $this->dberror();

This will resolve this issue, and force mysql to create a new connection.

I don't suggest you doing this if your server has a php version before 4.2.0

If this is the case, then I know of a dodgy fix to allow you to connect to one other database, but only one. If you have specified localhost as your server, then you can use "127.0.0.1" as a the host of your other connection. This will make php create a new link, as it thinks it's a different server (but it obviously isn't, as localhost and 127.0.0.1 is exactly the same.)

I hope this helps anyone with this issue. I'm pretty new to PHP and using MyBB, so if anyone sees something really wrong or dangerous with this, please tell me and I'll delete it straight away. I haven't had much time to test this, but I believe it is working great for me.

Hope it helps!