1

I have a main site at www.mydomain.com. I have a wordpress blog set up at www.mydomain.com/blog. I want visitors to my main site to see the 3 recent blog posts. I was able to pull it off easily using the following code:

<?php    
define('WP_USE_THEMES', false);
require('/home/mydomain/public_html/blog/wp-load.php');
query_posts('showposts=3');
?>

<?php while (have_posts()) : the_post(); ?>
      <div class='category rounded_box'>
        <?php if ( get_post_meta($post->ID, 'image', true) ) { ?>
        <div class="category-thumbnail">
            <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "image", $single = true); ?>&h=100&w=100&zc=1" width="100" height="100" border="0" /></a>
        </div>
        <?php } ?>
        <div class='category_title'>
            <h2><a href='<?php the_permalink() ?>'><?php the_title(); ?></a></h2>
        </div>
        <div class='duration_home'>
            <?php the_excerpt(); ?>
            <span>Posted on <?php the_time('F jS, Y') ?> - <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></span>
        </div>
      </div>
      <?php endwhile;?>

<?php
wp_reset_query();
?>

Problem now is, anything on the page that requires calling a table for the main site no longer works. I'm getting an error saying it is searching for that table in the Wordpress blogs database.

How do I break the connection to the Wordpress database?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
PaperChase
  • 1,557
  • 4
  • 18
  • 23
  • I have no idea about WP, never used it. It seems like using different value or just unset and re-set your db variable? Have you tried such way? Of course I'm assuming your main website or your main websites db is nothing to do with WP. Need to check those functions as well to be sure but I'm assuming (if I understood you correctly) these functions are resetting your db variable and sending it out as global var of course. – Revenant Jan 13 '12 at 01:37

2 Answers2

0

The OP wrote in an edit:

SOLVED!

I had to close the mysql connection, and then immediately after connect to my main database table. This is what I added to the end of the code:

mysql_close();
include "scripts/connect_to_mysql.php";
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • ([Solved in a question edit and converted to a community wiki](http://meta.stackoverflow.com/questions/267434/what-is-the-appropriate-action-when-the-answer-to-a-question-is-added-to-the-que)) – Brian Tompsett - 汤莱恩 Dec 18 '16 at 20:26
0

I know you fixed your issue, however, for future reference you should define each connection object when working with multiple connections. If your main database is on the same mysql server then you should be able to do a mysql_select_db($dbname); after you run the wordpress code; instead of closing and reopening the connection. For multiple connections follow the information from this answer: How do you connect to multiple MySQL databases on a single webpage?

Community
  • 1
  • 1
Jeff Wooden
  • 5,339
  • 2
  • 19
  • 24