WordPress.org

Ready to get started?Download WordPress

Forums

Can you perform an SQL query from this? (2 posts)

  1. ninjaboy81
    Member
    Posted 6 years ago #

    Great widget!

    But I need a way to perform a basic SQL query from your widget.

    This code here:
    1: <?php
    2: $querystr = "SELECT * FROM $wpdb->posts";
    3:
    4: $pageposts = $wpdb->get_results($querystr, OBJECT);
    5: ?>

    produces this error here:

    "Fatal error: Call to a member function get_results() on a non-object in ..path../wp-content/plugins/execphp/execphp.php(24) : eval()'d code on line 4"

    how could i make an SQL query work from your widget?

  2. ninjaboy81
    Member
    Posted 6 years ago #

    figured it out.

    you have to have global $wpdb; to access the db.

    so that's:

    1: <?php
    2: global $wpdb;
    3: $querystr = "SELECT * FROM $wpdb->posts";
    4: $pageposts = $wpdb->get_results($querystr, OBJECT);
    5: ?>

    works!

Topic Closed

This topic has been closed to new replies.

About this Topic