Support » Developing with WordPress » Can not get $wpdb working

  • Resolved gshell

    (@gshell)



    I am having trouble getting $wpdb to work from a plugin I am developing, or at all for that matter. I am new to WordPress and this is my first site development effort, so please excuse me for my basic questions. I suspect I have not initialized something or loaded something correctly to get it to function, but I’m not sure where.

    I started with creating a new website using the twenty seventeen theme on a localhost setup using the latest Bitnami wordpress stack. I then created a child theme to begin making the changes I wanted.

    In attempting to access some data from the database, I created the following code inside a new plugin. There is something wrong with the line defining $numberofusers using the $wpdb get_var function, as when it is included in the code wordpress crashes. Everything else in the plugin seems to work correctly. In fact, I don’t seem to be able to use any of the $wpdb functions anywhere (in a plugin, or a basic function, etc.). I added the require statement for wp-load.php to make sure wpdb was loaded.

    Thank you for any suggestions that you have.

    global $wpdb;
    require_once(ABSPATH . ‘wp-load.php’);

    function mmmp_get_member_info() {
    $numberofusers = $wpdb->;get_var( “SELECT COUNT(*) FROM $wpdb->;users;”);
    return “The number of rows in the users table are:” .$numberofusers;
    }

Viewing 5 replies - 1 through 5 (of 5 total)
  • catacaustic

    (@catacaustic)

    very awesome

    Your SQL string is wrong. Remove the ;’s that shouldn’t be there and it should work fine. Remmber, in SQL the ; means ‘end of query’, so everything after that is considered to be a new query.

    $numberofusers = $wpdb->get_var( "SELECT COUNT(*) FROM " . $wpdb->users);

    Thanks that is one of my issues with trying to learn how to do this, there is so much information on the internet that is out of date or does not work for a variety of reasons. It’s very time consuming to find the correct and current solutions. Any advice on quality reference materials for website and theme development would be greatly appreciated.

    As to the $wpdb functions. Removing the ;’s did resolve the crashing problem, however nothing is returned. I copied the code exactly as presented in the Codex Class Reference/wpdb with the exception of replacing ‘echo’ with return as ‘echo’ does not seem to work from an external function. The function now looks like this. I temporarily added a “Got Here” just before the $user_count assignment to prove that the function was operating as expected, and it properly returned the text. But no results from the $wpdb function.

    function mmmp_get_member_info() {

    $user_count = $wpdb->get_var( “SELECT COUNT(*) FROM $wpdb->users” );
    return “<p>User count is {$user_count}</p>”;
    }

    catacaustic

    (@catacaustic)

    very awesome

    You’re almost there.

    When you use $wpdb in a function like that, you need to “include” the $wpdb object in the function as it’s not passed in by default.

    Also, you can’t return an echo. You return a value, and echoing it out outputs it to the screen, so you’re doing two different, and incompattible, things there. What you do is return the value from the function ad echo it out somewhere else.

    function mmmp_get_member_info() {
        global $wpdb;
    
        $user_count = $wpdb->get_var( “SELECT COUNT(*) FROM $wpdb->users” );
        return $user_count;
    }
    
    echo '<p>User count: ' . mmmp_get_member_info() . '</p>'

    The first line of your function’s code should be global $wpdb;:

    function mmmp_get_member_info() {
    	global $wpdb;
    	$user_count = $wpdb->get_var(“SELECT COUNT(*) FROM $wpdb->users”);
    	return “<p>User count is {$user_count}</p>”;
    }

    Also, please use the “code” button when entering code in a post. It makes the code you enter much more readable.

    Thanks,
    I got it working. I had entered the global $wpdb at the beginning of the php file that will eventually contain several functions. I did not realize that I needed to enter it again inside of each function.

    Also I didn’t notice the ‘code’ button. Thanks for the insight. I will use it in the future.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.