Thursday, March 21, 2013

Switch between MySQL databases in PHP



Suppose we have db1 and db2 database in MySQL, we may need to switch between db1 and db2 often.
First we connect to MySQl database in PHP
$db mysql_connect('localhost''mysql_user''mysql_password');

Then we create a function switch_db and call PHP function mysql_select_db (note from PHP 5.5.0,  all PHP MySQL function  mysql_* will need to be change to mysqli_*

function switch_db($new_db){
   global $db;
   return mysql_select_db($new_db, $db);
}
 

to switch to db1:
  switch_db('db1');

No comments:

Post a Comment