Title: Accessing a non-wpdb database
Last modified: August 19, 2016

---

# Accessing a non-wpdb database

 *  Resolved [toomzee](https://wordpress.org/support/users/toomzee/)
 * (@toomzee)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/accessing-a-non-wpdb-database/)
 * I wrote a cool script (real basic) that pulls the top ten most recent posts from
   my phpBB forums and displays them on the front page of my WordPress site as a
   list of links. It works perfect if the phpBB site uses the same database as my
   WordPress site, which it does. Or, if I run the script stand-alone (outside of
   WP) from any domain on the same server. But my company also owns another forum
   that is on the same server but under another domain name, and when I try to pull
   data from that database from within WordPress, I get the error that “wordpress_db.
   phpbb_table does not exist” where wordpress_db is my WP database, obviously, 
   and phpbb_table is the table I am selecting data from over at the forum, in this
   case phpbb_topics. I have tried the new_link idea and it does not work, I have
   also tried mysql_close() – any ideas? Don’t make me use an inline frame! I looked
   at scriptygoddesse’s site but couldn’t get that to work. To do that, where do
   you change the syntax of the initial WP database query? What file is it in?
 * Here’s my script:
 *     ```
       <?php
   
       // Connects to the database //
       define ('DB_USER', 'phpbb_username');
       define ('DB_PASSWORD', 'phpbb_password');
       define ('DB_HOST', 'localhost');
       define ('DB_NAME', 'phpbb_database_name');
   
       $connect = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, new_link)
       or die(mysql_error());
   
       $db = @mysql_select_db(DB_NAME, $connect)
       or die(mysql_error()); 
   
       // Selects the data I want to display //
       $grabPosts = "SELECT topic_id, topic_title, topic_last_post_id from
       phpbb_topics ORDER BY topic_last_post_id DESC LIMIT 5";  
   
       $grabPosts_result= mysql_query($grabPosts)
       OR die('QUERY ERROR:<br />' .$grabPosts. '<br />' .mysql_error()); 
   
       // Puts that data into variables for use //
       while ($row = mysql_fetch_array($grabPosts_result)) {
       $topic_id = $row["topic_id"];
       $topic_title = $row["topic_title"];
       $topic_last_post_id = $row["topic_last_post_id"];
   
       // Trims long post titles down to 30 characters //
       $title_length = strlen($topic_title);
       if ($title_length > 30) {
       $title = substr($topic_title,0,30) . "...";
       }
       else {
       $title = $topic_title;
       }
   
       // Displays the list of 5 links //
       echo "<a href=\"http://www.timbercrawler.com/bb/viewtopic.php?p=" .
       $topic_last_post_id . "#" . $topic_last_post_id . "\">" . $title . "</a>
       <br />";
       }
   
       ?>
       ```
   

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

 *  Thread Starter [toomzee](https://wordpress.org/support/users/toomzee/)
 * (@toomzee)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/accessing-a-non-wpdb-database/#post-666492)
 * Bump.
 *  [Adam Brown](https://wordpress.org/support/users/adamrbrown/)
 * (@adamrbrown)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/accessing-a-non-wpdb-database/#post-666494)
 * You can’t do this:
 *     ```
       define ('DB_USER', 'phpbb_username');
       define ('DB_PASSWORD', 'phpbb_password');
       define ('DB_HOST', 'localhost');
       define ('DB_NAME', 'phpbb_database_name');
       ```
   
 * Those are the same constant names that WP uses. If you’re trying to execute this
   script from within WP, you’ll need to use unique variable and constant names.
 * Side note. Wouldn’t it be more efficient to grab the PHPBB feed using MagpieRSS?
   That way, the 10 most recent topics would be cached for 1 hour by magpie rather
   than having to execute this query on every page load.
 *  Thread Starter [toomzee](https://wordpress.org/support/users/toomzee/)
 * (@toomzee)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/accessing-a-non-wpdb-database/#post-666500)
 * I want it reloaded every time they refresh. Our forums get over 200,000 page 
   views per day and our main website gets half that, so we want as many chances
   to catch their eye with a unique post title and get them to the forum as possible…
   These two sites are on a dedicated Linux server so there’s no worry on traffic
   or load.
 * Where does WP call its database connection, what file is it in?
 *  [Adam Brown](https://wordpress.org/support/users/adamrbrown/)
 * (@adamrbrown)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/accessing-a-non-wpdb-database/#post-666504)
 * > Where does WP call its database connection, what file is it in?
 * The db stuff is in wp-includes/wp-db.php or something. but before you fiddle 
   with that, give your constants unique names and see if that fixes the problem.
   Really. This is a big problem.
 *  Thread Starter [toomzee](https://wordpress.org/support/users/toomzee/)
 * (@toomzee)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/accessing-a-non-wpdb-database/#post-666508)
 * Okay that works, resolved. Thank you Adam. I just changed the database variables:
 * define (‘PHPBB_USER’, ‘username’);
    define (‘PHPBB_PASSWORD’, ‘password’); define(‘
   PHPBB_HOST’, ‘localhost’); define (‘PHPBB_NAME’, ‘dbname’);
 * $connect = @mysql_connect(PHPBB_HOST, PHPBB_USER, PHPBB_PASSWORD)
    or die(mysql_error());
 *  $db = @mysql_select_db(PHPBB_NAME, $connect)
    or die(mysql_error());
 *  [fatalseo](https://wordpress.org/support/users/fatalseo/)
 * (@fatalseo)
 * [18 years ago](https://wordpress.org/support/topic/accessing-a-non-wpdb-database/#post-666782)
 * This is really? No conflicts?
 *     ```
       if ( ! isset($wpdb) )
       	$wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
   
       if ( ! isset($wpdbBlog2) )
       	$wpdbBlog2 = new wpdb(DB_USER_BLOG2, DB_PASSWORD_BLOG2, DB_NAME_BLOG2, DB_HOST_BLOG2);
       ```
   
 *  [ivovic](https://wordpress.org/support/users/ivovic/)
 * (@ivovic)
 * [18 years ago](https://wordpress.org/support/topic/accessing-a-non-wpdb-database/#post-666783)
 * FatalSEO, I just love your username.
 * optimise or die!.. Survivor: Google.

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

The topic ‘Accessing a non-wpdb database’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 4 participants
 * Last reply from: [ivovic](https://wordpress.org/support/users/ivovic/)
 * Last activity: [18 years ago](https://wordpress.org/support/topic/accessing-a-non-wpdb-database/#post-666783)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
