Support » Plugins » $wpdb Fatal error

  • I’m tring to write a plugin and I’m having trouble using the database. I’m doing the following:
    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/>”;
    echo “$str not in database<br/>”;
    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?

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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.

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘$wpdb Fatal error’ is closed to new replies.