0

I have two records in my custom table. I am trying to use SELECT COUNT(*) AS total FROM tableA but I am not getting the results I am expecting.

The code below will echo Array ( [0] => Array ( [total] => 2 ) ):

// Count the amount of records in the table
$total = $wpdb->get_results( "SELECT COUNT( * ) AS total FROM tableA", 'ARRAY_A' );

echo "Total Records:" . print_r( $total );

The code below echos nothing:

// Count the amount of records in the table
$total = $wpdb->get_results( "SELECT COUNT( * ) AS total FROM tableA", 'ARRAY_A' );

echo "Total Records:" . $total[0]['total'];

How can I simplify this? What am I doing wrong? I'm racking my brain over this and I just can't get it to work.

1 Answer 1

0

You can simplify that by using $wpdb->get_var.

$total = $wpdb->get_var( "SELECT COUNT( * ) AS total FROM tableA" );
echo "Total Records:" . $total;

However, the code you've got should work-- either version of it. I tested both.

The only thing I notice is that the first version of the code should give you Array ( [0] => Array ( [total] => 2 ) ). Where is cnt coming from?

2
  • Thanks for coming to my rescue again! Sorry, the cnt is a mixup. I've tried using the $wpdb->get_var() method, and it still returns as blank. I don't get it. Oct 14, 2013 at 5:53
  • Sorry, I got it to work! I forgot to take out the , ARRAY_A when I changed the $wpdb methods. Oct 14, 2013 at 6:17

Your Answer

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

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