We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,420 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

PHP MySQL Help - Setting a database

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 :)

2
Contributors
2
Replies
2 Hours
Discussion Span
1 Year Ago
Last Updated
3
Views
Question
Answered
Wolxhound90
Junior Poster in Training
61 posts since Mar 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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.

diafol
Keep Smiling
Moderator
10,675 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

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 :)

Wolxhound90
Junior Poster in Training
61 posts since Mar 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 1 Year Ago by diafol

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1068 seconds using 2.69MB