• Alright, I’m trying to do something really stupid here, I have a Page set up to use a particular custom template I’ve created.

    This custom template includes some other PHP file after it displays the Page content. This PHP file (among other things) makes a database connection. Somehow, this database connection replaces the connection that WordPress was using.

    If I don’t close the connection, I see “Table doesn’t exist” errors, and the error indicates it’s looking for the table in the database that this other PHP file used. If i DO close the connection, It simply displays nothing, as if it couldn’t retrieve the data (which, if it’s looking at a closed connection, makes sense).

    So, to make this long explanation short: Any way to fix this? is this poor programming in WordPress, or simply a limitation of mySQL (which doesn’t seem like it is)?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter nighthawkthesane

    (@nighthawkthesane)

    Ahh. I see. I probably would have figured that out eventually, but I’m far to lazy to go through all that debugging 🙂

    Thanks. That’ll be a problem when using an abstraction layer (as all good programmers should), but I think i can work around it.

    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:

    $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:

    $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);

    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Using Other Databases in Templates’ is closed to new replies.