Title: accessing external database
Last modified: August 20, 2016

---

# accessing external database

 *  Resolved [clairence](https://wordpress.org/support/users/clairence/)
 * (@clairence)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/accessing-external-database/)
 * Oddly, I used the search function and it returned zero results, although I’m 
   sure there are many questions about this.
 * In any case… I want to be able to call an external database from inside a page
   and/or post. How do I do that?
 * I tried the following in a page (using ‘allow php in posts and pages’ plugin),
   and get errors indicating “no database selected”:
 *     ```
       [php]
   
       function main_db_login() {
       $passwrd='secretpw';
       $user='mysitemain';
       $db='mysite_main';
       $dbcnx = mysql_connect('localhost',$user,$passwrd);
       if (!$dbcnx) {
       echo '<p>Unable to connect to the WordPress DB at this time.</p>';
       exit();
       }
       }
   
       function contact_db_login(){
       $passwrd='secretpw';
       $user='mysitecontact';
       $db='mysite_contact';
       $dbcnx = mysql_connect('localhost',$user,$passwrd);
       if (!$dbcnx) {
       echo '<p>Unable to connect to the contacts DB at this time.</p>';
       exit();
       }
       }
   
        function users_db_login(){
       $passwrd='secretpw';
       $user='mysiteusers';
       $db='mysite_users';
       $dbcnx = mysql_connect('localhost',$user,$passwrd);
       if (!$dbcnx) {
       echo '<p>Unable to connect to the users DB at this time.</p>';
       exit();
       }
       }
       [/php]
   
       [php]
   
       mysql_close();
       contact_db_login();
       $dabble=mysql_query("select * from messages");
       while($row=mysql_fetch_array($dabble)){
       	print_r($row);
       }
       mysql_close();
       echo'closed';
   
       users_db_login();
       $dibble=mysql_query("select * from basic");
       while($row=mysql_fetch_array($dibble)){
       	print_r($row);
       }
       mysql_close();
       echo'closed';
       main_db_login();
   
       [/php]
       ```
   
 * Any help is appreciated.

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/accessing-external-database/#post-2199497)
 * So you are trying to connect to multiple databases that are hosted on the _same
   physical machine_ on which this script runs?
 *  Thread Starter [clairence](https://wordpress.org/support/users/clairence/)
 * (@clairence)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/accessing-external-database/#post-2199498)
 * yes.
 *  [fonglh](https://wordpress.org/support/users/fonglh/)
 * (@fonglh)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/accessing-external-database/#post-2199508)
 * exactly what the error message says. you haven’t selected a database.
    [http://php.net/manual/en/function.mysql-select-db.php](http://php.net/manual/en/function.mysql-select-db.php)
 *  Thread Starter [clairence](https://wordpress.org/support/users/clairence/)
 * (@clairence)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/accessing-external-database/#post-2199509)
 * So many times, I looked at the error message, and looked at the script… and just
   didn’t see how I wasn’t selecting a database. But I see you’re right; I was logging
   in but not to a specific database.
 * How foolish of me.
 * Thanks for the nudge.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘accessing external database’ is closed to new replies.

## Tags

 * [external database](https://wordpress.org/support/topic-tag/external-database/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 3 participants
 * Last reply from: [clairence](https://wordpress.org/support/users/clairence/)
 * Last activity: [14 years, 10 months ago](https://wordpress.org/support/topic/accessing-external-database/#post-2199509)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
