Title: Database connections
Last modified: August 21, 2016

---

# Database connections

 *  [Jonathan Perlman](https://wordpress.org/support/users/jperlman/)
 * (@jperlman)
 * [13 years ago](https://wordpress.org/support/topic/database-connections-1/)
 * Hi, I have a custom template which pulls data from a seperate source, not the
   wordpress database.
 * What is the best way to encapsulate / do the database connection so that there
   are no conflicts with wordpress?
 *     ```
       <?php
       /*
         Template Name: High Run
        */
       ?>
   
       <?php get_header(); ?>
   
       <div id="container">
   
           <div id="content" role="main">
   
               <?php if (have_posts()) while (have_posts()) : the_post(); ?>
   
                       <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
   
                           <h1 class="entry-title"><?php the_title(); ?></h1>
   
                           <div class="entry-content">
                               <?php the_content(); ?>
   
       			<?php
       				// database connect
       				mysql_connect($dbhost, $user, $password);
       				mysql_select_db($database);
   
       				$sql = "select * from $database.$table";
       				$results = mysql_query($sql);
   
       				if ( mysql_num_rows($results) > 0 ) {
   
       					print "<table><thead>";
       					print "<tr>
       					<th>Name</th>
       					<th>Province</th>
       					<th>High Run</th>
       					</tr>
       					</thead>
       					<tbody>";
   
       					while( $record = mysql_fetch_array($results)) {
       						print "<tr>";
       						print "<td>" . $record['name'] . "</td>";
       						print "<td>" . $record['location'] . "</td>";
       						print "<td>" . $record['value'] . "</td>";
       						print "</tr>";
       					}
   
       					print "</tbody></table>";
   
       				} // if mysql_num_rows
   
       			    } // end else
                               ?>
   
                           </div><!-- .entry-content -->
                       </div><!-- #post-## -->
   
                   <?php endwhile; ?>
   
           </div><!-- #content -->
       </div><!-- #container -->
   
       <?php get_footer(); ?>
       ```
   

The topic ‘Database connections’ is closed to new replies.

## Tags

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

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 0 replies
 * 1 participant
 * Last reply from: [Jonathan Perlman](https://wordpress.org/support/users/jperlman/)
 * Last activity: [13 years ago](https://wordpress.org/support/topic/database-connections-1/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
