Forums

Getting data from using $wpdb from my created table (3 posts)

  1. burrowsja
    Member
    Posted 1 year ago #

    This should be simple. I copied and pasted the below code from the codex page on $wpdb into a template and it returns the correct $user_count variable:

    <?php
    $user_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->users;"));
    echo '<p>User count is ' . $user_count . '</p>';
    ?>

    I've created a table (wp_designers) and tried to call exactly the same query as above on this table but it returns nothing:

    <?php
    global $wpdb;
    $user_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->designers;"));
    echo '<p>User count is ' . $user_count . '</p>';
    ?>

    Can someone please tell me what I'm missing?

  2. Stefan M.
    Member
    Posted 10 months ago #

    Not part of my plugin.

  3. Stefan M.
    Member
    Posted 10 months ago #

    Problem is that $wpdb->designers is not known.

    global $wpdb;
    $user_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM ".$wpdb->prefix."designers;"));
    echo '<p>User count is ' . $user_count . '</p>';

    use this ;)

Topic Closed

This topic has been closed to new replies.

About this Topic