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:

$database=$GET['db'];

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 for diafol

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

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

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

if(isset($try_db)){
   $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());
  }else{
    $_SESSION['db'] = $use_db;
  }
}else{
   $_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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.