num_rows always returns 1

global $wpdb;

$wpdb->get_results("SELECT * FROM wp_ap_promo WHERE wp_ap_promo.promocode = '".$promocode."' AND wp_ap_promo.business_id = " . $id .";");
echo $wpdb->num_rows;

My num_rows always gives a 1, even though there are no results.

EDIT: seems like the problem was in my SQL-tables. But accepted answer also seems to work.

Related posts

Leave a Reply

1 comment

  1. Try to count() the values instead.

    Like this:

    global $wpdb;
    
    $test = $wpdb->get_results("SELECT * FROM wp_ap_promo WHERE wp_ap_promo.promocode = '".$promocode."' AND wp_ap_promo.business_id = " . $id .";");
    echo count($test);
    

    if it still does not work, use something like this:

    if($test != null) {
       echo count($test);
    } else {
       echo 0;
    }
    

    Hope this helps! 😀