WordPress.org

Forums

$wpdb->num_rows incorrect (4 posts)

  1. robahas
    Member
    Posted 2 years ago #

    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;
  2. Chris
    Member
    Posted 2 years ago #

    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 ) );
  3. robahas
    Member
    Posted 2 years ago #

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

  4. c_cav
    Member
    Posted 2 years ago #

    $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}");

Topic Closed

This topic has been closed to new replies.

About this Topic