Skip to main content
Corrected a key from `cnt` to `total`
Source Link

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 ( [cnt][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.

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 ( [cnt] => 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.

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.

Source Link

wpdb Cannot Access Associative Array Data in a Count Query

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 ( [cnt] => 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.