Skip to content
Advertisement

Connecting to multiple MySQL database in single php app [closed]

I’m trying to connect multiple mysql databases to a single php web app. I’d like each user to be able to access a different database. Every database has same tables but I wouldn’t like userA to na able to access the data of userB, using the same app.

How do I do this for the user to login to their own database. I saw this below online which addressed similar tasks but I don’t know how to implement login into a database.

E.G.

if(user1){
  User1_db
}elseif(user2){
    User2_db
}

I need your help pls. Thanks

Advertisement

Answer

I would use the main database where I store user databases. And then you could fetch user DB from the main database and connect. Pseudo code:

$user = $mainDbConnection->select(select * from users where id = 'user_id');

$userDbConnection = $db->connect($user->database);

 // And then use this connection...

$someDataFromUserDb = $userDbConnection->select('select ...');
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement