I am new to wordpress development & tried to learning plugin development.I have created a custom table from which i want to show the number of records found.I have tried the below code but it always shows 1 as a result irrespective of number of rows in a table.

//To show number of rows in table
    function DB_Tables_Rows()
    {
      global $wpdb;
      $table_name = $wpdb->prefix . 'mydata';
     $count_query = "select count(*) from $table_name";
         $num = $wpdb->get_var($count_query);

         $num = $wpdb->num_rows;

        echo  $wpdb->num_rows . 'Rows Found';


    }

2 Answers
2

Why don’t you directly echo the $num as it will contain the count of rows already…Here’s the edited part..

//To show number of rows in table
  function DB_Tables_Rows()
{
    global $wpdb;
    $table_name = $wpdb->prefix . 'mydata';
    $count_query = "select count(*) from $table_name";
    $num = $wpdb->get_var($count_query);

    echo  $num . 'Rows Found';


}

Leave a Reply

Your email address will not be published. Required fields are marked *