Wordpress function/plugin to count number of rows in mySQL table

admin

Administrator
Staff member
I have additionals table in my Wordpress database that contain a lists of companies, link, description, etc. I'm using this plugin (<a href="http://wpdatatables.com/" rel="nofollow">http://wpdatatables.com/</a>) to render the table in WP and it works great.

One feature it doesn't have is a way to count of the number entries (rows) in the table. I'd like to get this count and display it in a page on wordpress. For example, count all the rows in table X and display here (with a shortcode or something else) so I could have "TABLE X has 1,912 companies".

I came across this as a way to get started, but not sure if it'll work for what I need and then I'm not sure how to actually get the result into a post/page.

Code:
&lt;?php
$myvar = $wpdb-&gt;get_results("SELECT COUNT(*) FROM company_table WHERE name=*");

if($myvar == 0)
{
// no rows, do whatever.
}
else
{
// at least 1 row, do something else.
}
?&gt;

or this:

Code:
global $wpdb;
$wpdb-&gt;get_results( 'SELECT COUNT(*) FROM table_name' );
echo  $wpdb-&gt;num_rows . 'Rows Found';

How can I get the row count and display it on a page from this point (or some other recommended started point)?

<strong>UPDATE</strong>

Okay, I made a plugin with a shortcode like this and can get it to appear in my page correct, but it's rendering the wrong number of rows. It's only showing 1 row, when it should be several hundred.

Code:
function row_count_shortcode() {
global $wpdb;
$wpdb-&gt;get_results( 'SELECT COUNT(*) FROM inno_db' );
echo  $wpdb-&gt;num_rows . 'Rows Found';
}
add_shortcode( 'row_count', 'row_count_shortcode' );

Any idea what the issue might be?

Thanks