Connecting to an external database from within a Wordpress post

For the article I wrote recently on web design galleries, I compiled a table of the galleries in a MySQL database. To be able to display the table and the associated charts and other statistics that went with it, I needed to be able to connect to this external database from within Wordpress.

A post on the Wordpress support forum suggested creating a separate template and adding an include which contained the database connection string to that template. That’s what I did for the page to display the table as the design for the page called for a single column rather than two like the rest of the site, but you can only assign different templates to pages, not posts, within Wordpress; posts in Wordpress are all displayed by single.php. That wasn’t an issue though because I just placed a conditional at the top of the single template that said if(is_single(XXX)) include('database_connection.php');.

That worked fine for the post, but what I didn’t realise at first was that it had broken the site’s feed because the database connection include wasn’t available to the files that create the feed which can be found in /wp-includes/ – feed-atom.php, feed-rdf.php, feed-rss.php and feed-rss2.php. So when the feed file was parsing the post and came to a call to a PHP function contained in an include file that it didn’t have access to, it threw an error.

The solution to this was simple too though: just take the include calls out of the template and include them in the actual post themselves (before everything else) as everything included in the post gets passed to the feed-creating files. I was already using the Exec-PHP plugin to enable the use of PHP within my posts, so it just meant linking to the includes from a different part of the page; a part of the page to which the feed files had access.

Browse by tags:

Tags: , ,

Share this article:
  • Twitter
  • FriendFeed
  • del.icio.us
  • Digg
  • StumbleUpon
  • Facebook
  • Reddit
  • Sphinn

Subscribe to this site for regular updates

12 responses to Connecting to an external database from within a Wordpress post. Add your own.

Comments

  1. 1

    Okay sooo how did you do this? Any examples I could find to help with something that I am currently working on?

    - Phil

  2. 2

    Hi Phil,

    At the top of the post, I included:


    <?php
    include('/path/to/database/connection.php');
    ?>

    and that file looked like:


    <?php
    function runSQL($rsql) {
    $rootpasswd='yourPasswordHere';
    $user='yourUserHere';
    $db='yourDBhere';
    $dbcnx = @mysql_connect('localhost',$user,$rootpasswd,true);
    if (!$dbcnx) {
    echo '<p>Unable to connect to the database server at this time.</p>';
    exit();
    }
    mysql_select_db($db, $dbcnx);
    $result = mysql_query($rsql) or die ('test');
    return $result;
    mysql_close($connect);
    }
    ?>

    Then to loop through the results:


    $sql = "Your SQL statement goes here";
    $result = runSQL($sql);
    while ($row = mysql_fetch_array($result)) {
    Do stuff;
    }

  3. 3

    Totally awesome!
    I was looking for something like this. Want to turn it into a function to call my database options on a custom theme I released.

    Now to figure on how to implement

  4. 4

    hi,

    I still get the error

    Warning: mysql_error(): 10 is not a valid MySQL-Link resource

    which is triggered differently.

    I just wonder how to debug such an error since it points to a sub sub function.

  5. 5

    I was able to solve my problem modifying the function on my database manipulation to include the mysql_select_db function and commenting the mysql_close function safely.

  6. 6

    You really want to use one of the Wordpress path constants in order to include your custom php file.

    http://codex.wordpress.org/Determining_Plugin_and_Content_Directories

  7. 7

    That’s assuming the database connection file resides within your Wordpress directory structure, which might not necessarily be the case.

  8. 8

    every thing is perfectly OK .but i don’t understand, in which editor do i modify this?

  9. 9

    please give me the procedure if i am implementing dB for a login form for both admin remote server as well as localhost.

  10. 10

    Hi Ram, I’m sorry but I don’t understand the question.

Pingbacks

  1. 1

    […] Go to the author’s original blog: Connecting to an external database from within a Wordpress post […]

  2. 2

Feed for this post's comments


Required indicates required field.
Email will not be published

You can use these tags in your reply:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Leave a Reply

Contact details