Dony
Dony

Reputation: 212

Multiple Database Connections For One App

Im a using this php mailing app and Im trying to have multiple databases under one folder without having to repeat this over and over. The db connection is using smarty template and im not quite sure how to go about this.

Application files: http://pommo-ext.googlecode.com/svn/trunk/

I'm trying to do this:

If username/password is Smith => connect to DB_1 
If username/password is John => connect to DB_2 
If username/password is Jim => connect to DB_3

This is my config.php file for the DB connector:

http://pommo-ext.googlecode.com/svn/trunk/config.sample.php

[db_hostname] = localhost
[db_username] = root
[db_password] = root
[db_database] = db_1
[db_prefix] = db_

Also here is the class for the DB as well. http://pommo-ext.googlecode.com/svn/trunk/inc/classes/pommo.php

Upvotes: 0

Views: 841

Answers (1)

ralphje
ralphje

Reputation: 995

I'm not familiar with Pommo, but from your question I figure that you are trying to create some kind of multi-user environment with one installation of an application, but with different database settings. There's generally (for any application) no straight-forward or trivial way to do this.

First of all, you must rewrite all internal logic of Pommo to make sure that they use different database connections based on the username. The username, however, can't be stored in any database in that case, since the database must be selected based on the username. You could add another database for thsi information, but that's just silly.

Secondly, you want different databases, but I can't imagine why you'd want that. For starters, why not use table prefixes? Secondly, I'm sure that Pommo has some kind of multi-user environment, so that you can share the same installation with several users. Maybe that's not possible, but then you can do two things:

  • Start looking for some other application.
  • Start looking for some other enthausiast that already wrote what you want.

Finally, Smarty does not have to do anything with database connections. I imagine Pommo using Smarty for their templating or put Smarty templates in databases, but the actual database setup has to happen somewhere in PHP.


However, if you don't need some 'nice' solution, are prepared for serious ugliness and want a quick solution, you can probably use PHP sessions. You can store the username of the user in $_SESSION['custom_username'] by requesting it on a login page. Note however that you must have a login page that comes before Pommo tries to verify the username that was passed in its database, since the database is dependend on the username. I'd suggest a separate page independent of your Pommo installation. Maybe you want to create something like a file that uses a GET parameter, sets the session and forwards to the application.

After that, in the config.php, you can check for the username in the session and change the properties based on that.

For this to work, you must set up your PHP sessions properly and if Pommo already uses sessions, you can probably reuse that functionality, but make sure that it does not interfere with Pommo's usage of it.

Upvotes: 1

Related Questions