Support » Plugins » get_var() on a non-object $wpdb

  • I am trying to write a plugin that access the database but all I get is
    “Fatal error: Call to a member function get_var() on a non-object in C:\xampp\htdocs\wp-content\plugins\audio-comments\audio-comments.php on line 228”

    none of the examples work
    help
    // mt_comments_page() displays the page content for the audio comments Options submenu
    function mt_comments_page() {

    echo “<div class=\”wrap\”>”;
    echo “<h2>Audio Comments Manage</h2>”;
    // example code starts here
    $name = $wpdb->get_var(“SELECT cat_name FROM $wpdb->categories WHERE cat_ID=4”);
    echo $name;
    // example code ends here
    echo “</div>”;
    }

Viewing 9 replies - 1 through 9 (of 9 total)
  • Is this in a function? If so did you declare $wpdb as a global variable? If not then it doesn’t know what $wpdb is and doesn’t know what to do with it.

    grosbouff

    (@grosbouff)

    Hello, I have the same problem since a little time;
    but i HAVE the $wpdb as global…
    Don’t know what I’ve broken !

    function get_root_parent($page_id) {
    		global $wpdb;
    		$parent = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE post_type='page' AND ID = '$page_id'");
    		if ($parent == 0) return $page_id;
    		else return get_root_parent($parent);
    	}
    grosbouff

    (@grosbouff)

    I have this function inside my menu.php

    function get_root_parent($page_id) {
    	global $wpdb;
    	$parent = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE post_type='page' AND ID = '$page_id'");
    	if ($parent == 0) return $page_id;
    	else return get_root_parent($parent);
    }

    with this

    <div id="menu">
    		<?php
    			include (STYLEROOT.'/menu.php');
    		?>
    	</div>

    , I have the error

    : Call to a member function get_var() on a non-object in…

    , with this I have not…

    <div id="menu">
    		<?php
    function get_root_parent($page_id) {
    	global $wpdb;
    	$parent = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE post_type='page' AND ID = '$page_id'");
    	if ($parent == 0) return $page_id;
    	else return get_root_parent($parent);
    }
    			include (STYLEROOT.'/menu.php');
    		?>
    	</div>

    , but it seems that menu.php can’t find the function.
    What’s the deal ??!

    Kafkaesqui

    (@kafkaesqui)

    grosbouff,

    First, you’re better off placing PHP function statements in a separate functions.php file located with your active theme. This is included automatically by WordPress if it exists.

    Second, why are you referencing the function within the function at that point? The logic of your code would have:

    else return get_root_parent($parent);

    put PHP into a sort of dead loop (the function will just keep calling itself if there is no parent). I’d change it to:

    else return $parent;

    Finally, is the function actually called in your menu.php or elsewhere?

    grosbouff

    (@grosbouff)

    this function is called inside menu.php (which is a template file, like “sidebar”).
    I noticed that it works if I put all the content of menu.php inside header.php, without any include so.

    The inclusion is the problem…. what to do ?

    Thanks !

    Kafkaesqui

    (@kafkaesqui)

    I noticed that it works if I put all the content of menu.php inside header.php, without any include so.

    I don’t see how so. How does the function run if it is not called at some point? Just placing a PHP function statement into your code does not run it.

    grosbouff

    (@grosbouff)

    I put my function inside functions.php, it does not work either…

    function get_root_parent($page_id) {
    	global $wpdb;
    	$parent = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE post_type='page' AND ID = '$page_id'");
    	if ($parent == 0) return $page_id;
    	else return get_root_parent($parent);
    }

    (this is not a dead loop, the functions is recursive until $parent == 0)

    grosbouff

    (@grosbouff)

    (sorry, it works ! I forgot something…)

    What were you forgetting? I’m using this same code, and another function based off of it, and I’m trying to get it to work from within functions.php

    On a side note, should I declare the global $wpdb inside the function? Does it matter?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘get_var() on a non-object $wpdb’ is closed to new replies.