• Resolved 4mica

    (@4mica)


    Hello,

    I am setting up a site that pulls recent posts out of wordpress and shows them on the homepage. Everything was working great until I added some other functionality on the homepage. The other functionality pulls some content out of a database that’s not the wordpress database.

    In doing so, it selects another DB using @mysql_select_db($db). It looks like the wordpress code I have on my homepage doesn’t set the DB back to the wordpress database. Is there a simple way (without having to put the database server, db, user, password all into my code again) to reselect the WP database?

    Here’s what I’m currently using to pull the WP post onto the homepage:

    <?
    $posts = get_posts(‘numberposts=1&order=DESC&orderby=post_date’);
    foreach ($posts as $post) : start_wp();
    ?>
    <h2>” rel=”bookmark” title=”Permanent Link: <? the_title(); ?>”><? the_title(); ?></h2>
    <? the_excerpt_rss(); ?> ” rel=”bookmark” title=”Permanent Link: <?
    the_title(); ?>”>Keep Reading »

    <? endforeach;?>

    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter 4mica

    (@4mica)

    Thanks for getting back to me. What I ended up doing was just reestablishing the connection to the wordpress database on pages that had multiple databases, just by using:

    mysql_connect($server,$loginsql,$passsql);
    @mysql_select_db($base) or die( “Unable to select database”);

    I’m using the non WP database on many more pages than the wordpress database and it was less to change.

    I found an alternate solution for calling another MySQL database in a WordPress template. You need to use a new link. From Scriptygoddess’s original example:

    blockquote$databaseConnection = mysql_connect($databaseServer, $databseUsername, $databasePassword) or
    die (’I can’t connect to the database.’);
    mysql_select_db($databaseName,$databaseConnection);
    $query = “SELECT * from tablename;”;
    $result = mysql_query($query);blockquote

    You just need to add the new_link parameter in the mysql_connect call, which should work in 4.2 and above, like so:

    blockquote$databaseConnection = mysql_connect($databaseServer, $databseUsername, $databasePassword,new_link) or
    die (’I can’t connect to the database.’);
    mysql_select_db($databaseName,$databaseConnection);
    $query = “SELECT * from tablename;”;
    $result = mysql_query($query);blockquote

    It took me hours to find this solution, but it works perfectly for me – WordPress switches back to using its own database after the mysql_close() as it should. Hopefully this helps others in the future who are having the same problem and don’t want to have to define the database for each query.

    Good to hear.

    Trent

    bluedauber, many many thanks for that very useful tip!

    I’ve done this before, for example I was using XMB Forum with an add on that turned the homepage it into a sort of WordPress, this was way before WordPress was around – and I had some clever stuff set up to link to my other database and retrieve information, but the same problem occured then, when I connect to the other database, it doesn’t retain the first database (the XMB one) so the rest of the script after that point are broken!

    Exact same thing happened now I’m trying to do something similar in WordPress, but by adding the ‘new_link’ parameter to my second mysql_connect call, everything works as it should!!

    Thanks sooooo much 😀

    thebindlestick

    (@thebindlestick)

    what is the new_link supposed to be, the table name?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘resetting database connection back to WP db’ is closed to new replies.