0

My goal is to display information from an external (non-default) WordPress database using HyperDB. At this time information does not display in the generated table. Could someone provide an example on how to successfully access remote data using HyperDB and display the content?

(note: I have successfully displayed data from the same remote database by use of a new wpdb global variable. Thus the connection settings are correct.)

db-config.php

Here is the relevant source in db-config.php.

//STARTING WITH JUST DEFAULT WordPress DB
$wpdb->add_database(array(
    'host'     => DB_HOST,     // If port is other than 3306, use host:port.
    'user'     => DB_USER,
    'password' => DB_PASSWORD,
    'name'     => DB_NAME,
));

//NEW DATABASE CONNECTIVITY: Database: test_db Table: gen_nums, nojsontest, tbl_sample
$wpdb->add_database(array(
    'host'     => '127.0.0.3',     // If port is other than 3306, use host:port.
    'user'     => 'user',
    'password' => '',
    'name'     => 'test_db',
    'dataset'  => 'gen_nums', 'nojsontest', 'tbl_sample',
));

$wpdb->add_table( 'dataset', 'gen_nums' );
$wpdb->add_table( 'dataset', 'nojsontest' );
$wpdb->add_table( 'dataset', 'tbl_sample' );

hyperDB-Test.php

Using this source to display information pulled from the remote 'test_db' database 'gen_nums' table.

 function connectionThird( $args ){
         global $wpdb;
        //global $seconddb; //modified code for second database connection
        // Shortcodes RETURN content, so store in a variable to return
        $content = 'Test 03: HyperDB Example </br> Displaying remote database: "xxx" </br> from table: "xxx" ';
        $content .= '<table>';
        $content .= '<tr><td>07</td><td>08</td><td>09</td> </tr>';
        $content .= '<tr><th>ID</th><th>First Name</th><th>Last Name</th></tr>';
        $results = $wpdb->get_results( ' SELECT * FROM gen_nums ' ); //currently does not display data from remote (non-default WordPress Database)
        foreach ( $results AS $row ) {
            $content .= '<tr>';
            // Modify these to match the database structure
            $content .= '<td>' . $row->id . '</td>';
            $content .= '<td>' . $row->rand_num . '</td>';
            $content .= '<td>' . $row->volume . '</td>';
            $content .= '</tr>';
        }
        $content .= '</table>';

        // return the table
        return $content;
    }

add_action( 'wp_head', 'connectionThird' );
add_shortcode( 'hyperdb003', 'connectionThird' );

Thank you in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.