This topic is locked

Single project working with several identical databases

10/3/2012 6:10:09 PM
PHPRunner Tips and Tricks
admin

Lets consider the situation when you have a separate database for each client and need to be able to choose client's database on the fly. Here is how you can do this in PHPRunner.
In this example we use MySQL though the same idea will work for any other database.

  1. Define a new database server connection on 'Output directory' screen in PHPRunner. Add the new line of code at the end.

$host="localhost";

$user="root";

$pwd="";

$port="";

$sys_dbname="Jobs";

// if session variable is set we will connect to the database specified in this variable

// in other case we connect to default database

if ($_SESSION["dbname"]) $sys_dbname = $_SESSION["dbname"];


2. Define $_SESSION["dbname"] variable in AfterSuccessfulLogin or in AfterAppInit event.
Sample code:

if ($_SESSION["UserID"]=="admin")

$_SESSION["dbname"]="some database";

else ($_SESSION["UserID"]=="Bob")

$_SESSION["dbname"]="some other database";


3. You can take it a little bit further storing client's database name in one of the login table fields. More convenient when you have a lot of clients.
Here is the code for AfterSuccessfulLogin event that handles this situation:



// 'database' is the name of the field that stores database name

$_SESSION["dbname"]=$data["database"];