• Resolved redds013

    (@redds013)


    Morning,

    I was wondering if someone can offer me some help, I am trying a table out of my database and display in a wordpress page. I have looked at wpdb but can’t seem to get it working.

    i have put this in the template page
    $myrows = $wpdb->get_results( “SELECT * FROM hotels, OBJECT” );
    echo $myrows;
    But just getting the word “array” out but no data.

    The other night I got out
    Catchable fatal error: Object of class stdClass could not be converted to string.

    So I am not sure what am doing wrong.

    thanks in advanced

Viewing 4 replies - 1 through 4 (of 4 total)
  • $myrows will be an array. You can dump it out with print_r($myrows) to see what is in it.

    Your query is not coded correctly. The word ‘OBJECT’ should not be inside the quotes.

    Besides that, unless ‘hotels’ is a table that you created outside of WordPress, there will not be a table by that name. Please explain what you are trying to do so the proper query can be determined.

    Especially explain if ‘hotels’ is a Category, a Custom Post type, or something else.

    To print things from the array, you use a foreach loop, something like this:

    $myrows = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_type = 'parks'", OBJECT);
    //print_r($myrows);
    global $post;
    foreach ( $myrows as $post ) {
       setup_postdata($post);
       echo "<p>TITLE:"; the_title(); echo "</p>";
    }
    Thread Starter redds013

    (@redds013)

    Thank you for your reply. I really appreciate it.

    Okay So I have a list of hotels in the area in the database in a table called hotels.

    I want to be able to call out the hotels that are in the table and display them on a wordpress page.

    Each hotel has an id, name, address, cost and picture. I want to display it in a table format too.

    Thanks again.

    I can’t test this as I have no ‘hotels’ table in my test database, so it may require some changes.

    $myrows = $wpdb->get_results( 'SELECT * FROM hotels', OBJECT);
    //print_r($myrows);
    if ($myrows) {
       foreach ( $myrows as $hotel ) {
          echo "<p>ID:$hotel->id NAME:$hotel->name COST:$hotel->cost</p>";
       }
    }

    I leave the formatting of the output up to you.

    If you don’t get any output, uncomment the print_r() line to see if you are getting any results.

    Thread Starter redds013

    (@redds013)

    Thank you so much, You are an absolutely angel. Its works fantastically.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WPDB’ is closed to new replies.