• Resolved 44nato44

    (@44nato44)


    I am trying the code below, and I just cannot get it to work with the wordpress tables or my customer tables.

    — My code —

    [insert_php]

    global $wpdb;
    $wpdb->show_errors();

    $user_count = $wpdb->get_var( “SELECT COUNT(*) FROM $wpdb->users” );

    $wpdb->print_error();
    echo “Test”;
    [/insert_php]

    — The Error Message/Output —

    WordPress database error: []
    SELECT COUNT(*) FROM wp_users

    Test

    Can it be my DB which is corrupt, is it my theme maybe, or something else ?

    I am a bit lost and not sure how to troubleshoot this.

    Thanks for any advice

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello 44nato44,

    There is no any error in your above code , the WordPress database error: [] shows empty array. If have any error in your query syntax then the WordPress database error array will show like below

    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘wp_users’ at line 1]

    Example:

    global $wpdb;
     $wpdb->show_errors();
     $user_count = $wpdb->get_var( "SELECT COUNT(*) fFROM $wpdb->users" );
     $wpdb->print_error();
     echo "<p>User count is {$user_count}</p>";

    then it will show database error because there is error in my syntax “SELECT COUNT(*) fFROM $wpdb->users”,
    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘wp_users’ at line 1]

    so don’t worry your code is fine and your db is not corrupt.
    i hope it will help you.

    Thanks

    Thread Starter 44nato44

    (@44nato44)

    Thanks cedcommerce,

    I was so focused on the “error”, I did the below and both returned a result.

    $user_count = $wpdb->get_var( “SELECT COUNT(*) FROM $wpdb->users” );

    echo “<p>User count is {$user_count}</p>”;

    $building_count = $wpdb->get_var( “SELECT COUNT(*) FROM tbl_Buildings” );

    echo “<p>Building count is {$building_count}</p>”;

    //$wpdb->print_error();

    Thanks for clarifying 🙂

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Unable to use $wpdb’ is closed to new replies.