This topic is locked
[SOLVED]

 Changing Connection dynamically after login

7/22/2019 3:39:19 AM
PHPRunner General questions
T
tisento author

I wonder if it is possible to dynamically change the database connection and display the data in a single list page.
For example: Customer A has Connection A, Customer B has Connection B. Both customer data are stored in two identically structured databases A and B. All table names and headers have same names, only the database is called different.
I need such a solution to be able to process separate data for clients. And I do not want to create an idenfull list page for every customer, but just the same listpage with a different connection.
The event "after application initialized" I can not use it, but the change of the connection can take place only after login and subsequent selection of the client.
But I do not want a login per client, but the workflow should be: Login ==> Select client ==> Listpage
Where do I have to change the connection so I can work with a single listpage?
I have the latest phprunner enterprise version.

T
tisento author 7/30/2019

Hi,
your solution works in the output, but what would the "After Application Initialized" event look like, which we would prefer for database switching?

  1. with DB:SetConnection



DB::SetConnection ($_POST ['CustomerDBName']. " at localhost");


2) like in output



if ($_GET["dbname"])

$_SESSION["dbname"] = $_GET["dbname"];
$Host = "localhost";

$User = "root";

$Pwd = "";

$Port = "";

$Sys_dbname = $_SESSION ["dbname"];



No matter what we tried, both solutions do not work in the "After Application Initialized" event. What are we doing wrong?

admin 7/30/2019

Use the method offered in the article, just make sure you cope and paste the code instead of typing it manually. In PHP $host and $Host are two different things.

T
tisento author 8/7/2019

Hello Sergey,

yes, changing the DB connection in the output works, but ...
1.)

If I save tables from different DBs (example DB1 and DB2) as a template and import them into my main project, then all tables are loaded into the DB with the primary connection, regardless of whether the tables come from DB1 or DB2. The second connection is lost.
As a result, we must make manual changes in the code of the main project and manually import the tables from DB2 into main project.
What are we doing wrong?
2.)

If I want to save a subproject as a template, I am asked if I want to copy all the tables in the database.
No matter if I say yes or no:

when importing the template it says: "Database already contains tables ... all tables in this template wants to be updated and existing data will be lost.
As a result, all data already collected will be deleted if I want to re-add the template after making changes.
Is not it possible to export only the template without DB and then import it?
Or what am I doing wrong?
Help and thanks!

admin 8/7/2019

Why would you mix tables from DB1 and DB2? The whole point is that you can each database tables separately. You only need to add Database A to the project and then switch dynamically to Database B, Database C etc.