WordPress.org

Forums

$wpdb Fatal error (3 posts)

  1. moxy
    Member
    Posted 11 years ago #

    I'm tring to write a plugin and I'm having trouble using the database. I'm doing the following:
    code
    require_once '../wp-config.php';
    $title = 'Magic Decks';
    $this_file = 'magic-decks.php';
    $tablemagiccards = $table_prefix.'magiccards';
    $tablemagicdecks = $table_prefix.'magicdecks';
    function ensure_card($str) {
    $sql = "SELECT card_name FROM $tablemagiccards WHERE card_name = $str";
    $cards = $wpdb->get_results($sql);
    if ($cards) {
    foreach($cards as $card){
    $card->card_name = stripslashes($card->card_name);
    echo $card->card_name."<br/>";
    }
    }else{
    echo "$str not in database<br/>";
    }
    }
    code
    From the plugin I'm tring to learn from (recent-links) this seems like it should work but I'm getting the following error:
    Fatal error: Call to a member function get_results() on a non-object in C:\apachefriends\xampp\htdocs\moxy\wp-admin\magic-decks.php on line 12
    What is it that I missed?

  2. Cypher
    Member
    Posted 11 years ago #

    Change your code to the following:
    ...
    $result = $wpdb->query($sql);
    $cards = $wpdb->get_results();
    ...
    You can then continue to iterate on $cards and do your stuff.
    Regards

  3. Sebbi
    Member
    Posted 11 years ago #

    $wpdb ist a global variable which is not known inside functions ... you need to insert "global $wpdb;" after the line "function ensure_card($str) {" ...

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.