• am having difficulty in connecting to my db using $wpdb. having tried a lot of permutations, read thro the codex and forums and external sources and found none that solve the problem.

    i am using a local server (as you see a bitnami lamp set up). there are 6 databases i can view with phpmyadmin. i use only 2.

    the table i want to access is in a db called bitnami_wordpress in a table called wptable. there are 3 columns. no query using select has returned anything.

    in addition i am using the insert_php plugin to handle the php. (the plugin works fine for other php elements).

    the code below is just the final effort found. can anyone resolve this connection issue?

    $wpdb->select('bitnami_wordpress');
    $myrows = $wpdb->get_results( "SELECT col1 FROM wptable" );
Viewing 5 replies - 1 through 5 (of 5 total)
  • have you defined $wpdb variable as global

    global $wpdb;

    Thread Starter canyonal

    (@canyonal)

    thank you for responding.

    i have not defined global $wpdb, not as far as i can tell.

    where and how is it defined?
    on the relevant page. so where on the page does it sit and do i need to describe any variables or other items?

    $wpdb->select('bitnami_wordpress');
    $myrows = $wpdb->get_results( "SELECT col1 FROM wptable" );

    or is this put in another, but accessible file? if so how and where?

    I mean to say just put this line

    global $wpdb;

    above

    $wpdb->select('bitnami_wordpress');
    $myrows = $wpdb->get_results( "SELECT col1 FROM wptable" );

    Thread Starter canyonal

    (@canyonal)

    still not working.
    the code prints on the page.

    tried using <?php> opening and closing, also [insert_php], they hide the code but it doesnt function.

    is there some magic trigger to get this to function?

    i also having a similar problem and posted a support page: https://www.ads-software.com/support/topic/how-to-connect-multiple-databases-for-search-results

    Try this:

    1) When accessing the database in WordPress, you need to use the global variable “$wpdb”.

    $newdb = new wpdb($DB_USER, $DB_PASSWORD, $DB_NAME, $DB_HOST);
    $newdb->show_errors();

    2) Retrieve some data??? (which is another question, i guess)

    Add the code snippet to your functions.php file or a specific page template, and then you can use $newdb just like you would $wpdb to access a second database and retrieve some data.

    Source: https://bavotasan.com/2011/access-another-database-in-wordpress/

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘problem connecting with the $wpdb’ is closed to new replies.