8

I'm using the following code:

                    $wpdb->get_results("
                        SELECT * FROM " . $wpdb->prefix . "product_order 
                            WHERE 
                            rel = '" . $post["id"] . "' AND 
                            `range` = '" . $range . "' AND 
                            category = '" . $range . "'
                    "); 

                    echo $wpdb->num_rows;

num_rows returns 1 even though there is no rows in the database? Any ideas?

The variables I am putting in look fine. so it should be querying correctly.

2
  • What was the answer to this one? Jul 5, 2014 at 8:25
  • This is too long for me to remember... It's a shame you're obviously still having this problem. Perhaps a more stern search and a vote up might get you your answer :D Good luck buddy!
    – Jimmyt1988
    Jul 7, 2014 at 12:34

2 Answers 2

17
global $wpdb;
$wpdb->get_results("
                    SELECT * FROM " . $wpdb->prefix . "product_order 
                        WHERE 
                        rel = '" . $post["id"] . "' AND 
                        `range` = '" . $range . "' AND 
                        category = '" . $range . "'
                "); 

echo $wpdb->num_rows;

Now it returns numbers of rows select from above query and 0 if no row selected.....

0
2

if you JUST want the count (maybe for pagination total), it is faster to do:

global $wpdb;
$rows = $wpdb->get_results("
                    SELECT COUNT(*) as num_rows FROM " . $wpdb->prefix . "product_order 
                        WHERE 
                        rel = '" . $post["id"] . "' AND 
                        `range` = '" . $range . "' AND 
                        category = '" . $range . "'
                "); 

echo $rows[0]->num_rows;
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.