connect to two databases from the same script/page

Reply

Join Date: Apr 2009
Posts: 19
Reputation: odysea is an unknown quantity at this point 
Solved Threads: 0
odysea odysea is offline Offline
Newbie Poster

connect to two databases from the same script/page

 
0
  #1
May 12th, 2009
to connect/close connection my dbs i use the below include files

config.php
  1. <?php
  2. // Admin Database Properties
  3. $admindbhost = 'localhost';
  4. $admindbuser = 'user_default';
  5. $admindbpass = '************';
  6. $admindbname = 'db_admin';
  7.  
  8. // Default Database Properties
  9. $defaultdbhost = 'localhost';
  10. $defaultdbuser = 'user_default';
  11. $defaultdbpass = '************';
  12. $defaultdbname = 'db_default';
  13. ?>

opendb.php
  1. <?php
  2. // Admin Database Connection
  3. $admindbconnection = mysql_connect ($admindbhost, $admindbuser, $admindbpass)
  4. or die ('I cannot connect to the database because: ' . mysql_error());
  5. mysql_select_db ($admindbname);
  6.  
  7. // Default Database Connection
  8. $defaultdbconnection = mysql_connect ($defaultdbhost, $defaultdbuser, $defaultdbpass)
  9. or die ('I cannot connect to the database because: ' . mysql_error());
  10. mysql_select_db ($defaultdbname);
  11. ?>

closedb.php
  1. <?php
  2. // Admin Database Close Connection
  3. mysql_close($admindbconnection);
  4.  
  5. // Default Database Close Connection
  6. mysql_close($defaultdbconnection);
  7. ?>

then on the a php page on top i have the below

i include config.php and opendb.php here

  1. $query = "SELECT id, name, path FROM templates ORDER BY id";
  2. $result = mysql_query($query) or die('Error : ' . mysql_error());

i include closedb.php here

how can i get the connection to $admindbconnection or $defaultdbconnection please?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 66
Reputation: kronikmedia is an unknown quantity at this point 
Solved Threads: 1
kronikmedia kronikmedia is offline Offline
Junior Poster in Training

Re: connect to two databases from the same script/page

 
0
  #2
May 12th, 2009
You can also use a join query to query two tables at the same time. It is better yo do it in one query as opposed to using multiple connections.
Kronik Media: Web design Company in London, UK.

Our Websites:
Estate Agent Website
Search engine marketing & eCommerce Resources for business
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 849
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 138
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: connect to two databases from the same script/page

 
0
  #3
May 12th, 2009
  1. $result = mysql_query($query, $admindbconnection) or die('Error : ' . mysql_error());

http://php.net/manual/en/function.mysql-query.php
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC