-2

I am working with YII2.0 Multiple DB connection using ajax,i having multiple database like account, customer_1,customer_2..customer_n in account database having user table then each user in that table have the corresponding DB.

Based on the user_id i like to connect the DB using ajax.is there have any possibility to do this.

Thanks in advance for your idea and suggestion .

sen
  • 129
  • 5
  • 13
  • Well, I think you nedd to clarify your question. Ajax has nothing to do with DB-connections. Ajax just performs a request. The DB-connection is server-side. Are you trying to connect each user to a different db? If so, I have your answer... – PLM57 Mar 15 '16 at 19:09

1 Answers1

0

you can do something similar as below to create run time db connection. you need to get right dbname and other details from your main database to create below temp connection

//create temp db connection
            $config_temp = [
                'components' => [
                    'tempdb' => [
                        'class' => 'yii\db\Connection',
                        'dsn' => 'mysql:host=HOSTNAME;dbname=DBNAME',
                        'username' => USERNAME,
                        'password' => PWD,
                        'charset' => 'utf8',
                    ],
                ],
            ];
            $odb = Yii::createObject($config_temp['components']['tempdb']);
            //link user group for current org
            $sql = "SQL STATEMENT";
            $command_temp = $odb->createCommand($sql);
Insane Skull
  • 9,220
  • 9
  • 44
  • 63
NMT
  • 1
  • 1