This topic is locked

Switching Databases

5/19/2008 6:27:42 AM
PHPRunner General questions
M
marke author

I have an application which uses multiple replicated databases for live, test, and development. I would like to develop a PHPRunner project in such a way that I can switch to the relevant database within the project - rather than having to use separate projects for each database (and then maintain them).
Is this possible - what bits of the PHP would I need to change?
thanks
Mark

A
alang 5/19/2008

Sure - essentially you just redefine the global variable $conn;
global $conn, $host;
$conn = mysql_connect($host,$youruser,$yourpwd);

if (!$conn || !mysql_select_db($yourdbname,$conn))

{

trigger_error(mysql_error(), E_USER_ERROR);

}
where you substitute $youruser,$yourpwd,$yourdbname for your particular details. (assuming same host)
To restore original database connection (as built with PHPR), just use the following code:
global $conn, $host, $user, $pwd, $sys_dbname;
$conn = mysql_connect($host,$user,$pwd);

if (!$conn || !mysql_select_db($sys_dbname,$conn))

{

trigger_error(mysql_error(), E_USER_ERROR);

}
Put this code in events whereever you want to switch.