Search code examples
htmlmysqlwordpressphpbb

Get database results from phpbb into wordpress


I am trying to get the results from an external database for use in wordpress, the external DB is used for phpbb. I have a function which I know works correctly because echo $themeta[0]; returns what I want. But for some reason the DB select statement doesn't work giving me NULL when I try to do var_dump($forum_results). Has anyone got an idea on what I can do to fix this?

if ( ! function_exists( 'forum_database' ) ) :
/**
 * Forum database
*/
function forum_database() {

    $dbhost = 'localhost';
    $dbname = 'databasename';
    $dbuser = 'user2013';
    $dbpasswd = 'pass2013';
    $forum_table_b = 'phpbb_attachments';
    $forum_table_a = 'phpbb_topics';

    $forum_db = new wpdb($dbname, $dbuser, $dbpasswd, $dbhost);

    $key = 'forum_id';
    $themeta = get_post_custom_values($key, $post->ID);

    if( ! empty( $themeta ) ) {
        echo $themeta[0];

        $forum_results = $forum_db->get_results("SELECT topic_title FROM phpbb_topics WHERE forum_id = $themeta[0]");

        var_dump($forum_results);

        /*foreach ( $forum_results as $forum_result )
        {

            echo $forum_result->topic_title;
        }*/
    }
}
endif;

Solution

  • As you can clearly see the constructor of wpdb class first parameter is the dbuser then password then db name and last is host

    function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
            register_shutdown_function( array( $this, '__destruct' ) );
    
            if ( WP_DEBUG )
                $this->show_errors();
    
            $this->init_charset();
    
            $this->dbuser = $dbuser;
            $this->dbpassword = $dbpassword;
            $this->dbname = $dbname;
            $this->dbhost = $dbhost;
    
            $this->db_connect();
        }
    

    In your code you have pass the parameters in the wrong sequence

    $forum_db = new wpdb($dbname, $dbuser, $dbpasswd, $dbhost);
    

    shouldd be

    $forum_db = new wpdb($dbuser, $dbpasswd, $dbname , $dbhost);
    

    And to view the errors you can use $forum_db->show_errors(); or $forum_db->print_error();

    WPDB