943,719 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1496
  • PHP RSS
May 12th, 2009
0

connect to two databases from the same script/page

Expand Post »
to connect/close connection my dbs i use the below include files

config.php
PHP Syntax (Toggle Plain Text)
  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
PHP Syntax (Toggle Plain Text)
  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
PHP Syntax (Toggle Plain Text)
  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

PHP Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
odysea is offline Offline
22 posts
since Apr 2009
May 12th, 2009
0

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

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
kronikmedia is offline Offline
65 posts
since May 2008
May 12th, 2009
0

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

php Syntax (Toggle Plain Text)
  1. $result = mysql_query($query, $admindbconnection) or die('Error : ' . mysql_error());

http://php.net/manual/en/function.mysql-query.php
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 728
Bite my shiny metal ass!
pritaeas is offline Offline
4,166 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: how to query between months ranges
Next Thread in PHP Forum Timeline: Linking two MySQL Databases ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC