2

This question about wordpress cutom table.. I am creating a plugin I want to check a table named wp_school_post table empty or not.

See my code below which throws the following error

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\getwp\wp-content\plugins\getwp_P\schools_post_function.php on line 7

**filled** 

My code:

function schools_post_function()
{
//  echo "Sachool POST function";
    global $wpdb;
    $result = $wpdb->get_results("SELECT postid from wp_school_post WHERE `postid` IS NOT NULL");
    if(mysql_num_rows($result=='0'))
    {
        echo "not filled";
    }
    else
    {
        echo "filled";

    }
}
Savan Paun
  • 1,723
  • 2
  • 16
  • 24

3 Answers3

8

Try this :)

<?php
function schools_post_function()
{
    global $wpdb;
    $result = $wpdb->get_results("SELECT postid from wp_school_post WHERE `postid` IS NOT NULL");
    if(count($result) == 0)
    {
        echo "not filled";
    }
    else
    {
        echo "filled";

    }
}
?>
vivek
  • 316
  • 1
  • 4
  • 15
3

You could simplify your code and save resources using get_var instead to obtain the row count:

$count = $wpdb->get_var("SELECT COUNT(*)
    FROM {$wpdb->prefix}school_post WHERE postid IS NOT NULL");

if($count == 0)
{
    // No rows.
}
Marty
  • 39,033
  • 19
  • 93
  • 162
1

Using wordpress's WPDB class get_results() method will give you the data in form of object/array, you just need to check the count for $result which holds the result from get_results()

if(count($result) == 0){
// your code
}

See WPDB usage details

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118