Hey guys,

So, I have a connections php file which contains info such as the host, username, password and database to be accessed. I'm wondering if there is anyway to make the database value to be changed later on in the website. When first logging into the website they choose a database, and so I could use something like:


I am just wondering, if I used this, bearing in mind that the connections file will be included in all of the pages, would it then require for the database to be in the link string every time? If so, would it be possible to get around this somehow?

With the database possibly changing later on in the website too, the $GET method could be used when coming away from the page to change the database. It is just the concern that it would need to be included in all link strings in order to work properly.

Thanks in advance for any help :)

Member Avatar


Wasn't this covered in your previous thread? http://www.daniweb.com/web-development/php/threads/420748/saving-hyperlink-text-as-variable

$link = mysql_connect('localhost', 'your_username', 'your_password');
if (!$link) {
  die('Not connected : ' . mysql_error());

if(isset($_GET['db']) ){
   $try_db = $_GET['db'];
   $use_db = $_SESSION['db'];
  $use_db = 'database1'; //default

   $db_selected = mysql_select_db($try_db, $link);
if (!isset($db_selected) || !$db_selected) {
  $db_selected = mysql_select_db($use_db, $link);
  if (!$db_selected) {
    die ('Cannot link to a database : ' . mysql_error());
    $_SESSION['db'] = $use_db;
   $_SESSION['db'] = $try_db;

Off top of my head. It's late and I'm sure it could be more efficient.

Wow! That's a lot of if loops! I shall give it a go, but it looks as though it would work. Yeah, it was kind of covered in my last thread but my problem came when I was thinking about trying to use it in al of my pages and realising I would need to include ?db=Database1 at the end of each link, and I was just trying to find a bit of a better way around it :).

Thanks very much :)