WordPress.org

Support

Support » Plugins and Hacks » Hacks » $wpdb->num_rows incorrect

$wpdb->num_rows incorrect

  • Hi – Can anyone tall why in this code $wpdb->num_rows always returns “2” even when there are more rows? I’ve also tested it with SELECT *, in which case it returns 1. I’m really confused…

    $wpdb->get_results( 'SELECT COUNT(*) FROM $wpdb->rah_leads' );
    $rowCount = $wpdb->num_rows;
Viewing 3 replies - 1 through 3 (of 3 total)
  • That is weird. I think when doing a SELECT COUNT(*) $wpdb->num_rows should actually return 1 since there is only one result, the total.

    And I don’t know if that’s just a typo but it’s better to use single quotes like this 'SELECT COUNT(*) FROM ' . $wpdb->rah_leads

    Also, have you checked each single value?

    $sql      = 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'rah_leads';
    
    $results  = $wpdb->get_results( $sql );
    
    $rowCount = $wpdb->num_rows;
    
    var_dump( $sql, $results, $rowCount, count( $results ) );

    Thanks for the attempt Chris, but I figured out a different way to accomplish my goal so I’m letting this one go.

    $wpdb->get_results( ‘SELECT COUNT(*) FROM $wpdb->rah_leads’ );

    You are using single quotes, so $wpdb->rah_leads will not be evaluated by php. This will cause your SQL server to return an exception, which is probably why you always get 2 rows.

    $wpdb->get_results( “SELECT COUNT(*) FROM {$wpdb->rah_leads}”);

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘$wpdb->num_rows incorrect’ is closed to new replies.
Skip to toolbar