Title: check if table is empty
Last modified: August 19, 2016

---

# check if table is empty

 *  Resolved [dovydas](https://wordpress.org/support/users/dovydas/)
 * (@dovydas)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/check-if-table-is-empty/)
 * Hi.
 * I want to check if table is empty and then I want to insert some initial data.
   According to [http://www.mail-archive.com/sqlite-users@sqlite.org/msg19151.html](http://www.mail-archive.com/sqlite-users@sqlite.org/msg19151.html)
   the most efficient way to check if table is empty is this:
    `$wpdb->get_results("
   SELECT (SELECT id FROM $table LIMIT 1) IS NOT NULL AS x");` It gives me output:
 *     ```
       print_r($temp);
   
       Array ( [0] => stdClass Object ( [x] => 1 ) ) 1
       ```
   
 * How can I check if array element [‘x’] == 1 from php condition?

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/check-if-table-is-empty/#post-1281208)
 * If posts table is empty display message:
 *     ```
       <?php
       $empty = $wpdb->get_col($wpdb->prepare("SELECT 1 FROM $wpdb->posts LIMIT 1" ) );
       if ( empty($empty) ){
       echo 'Table is empty '. $empty;
       }
       ?>
       ```
   
 *  Thread Starter [dovydas](https://wordpress.org/support/users/dovydas/)
 * (@dovydas)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/check-if-table-is-empty/#post-1281221)
 * Thank you again Michael!
    You are wonderful. 🙂

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘check if table is empty’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [empty table](https://wordpress.org/support/topic-tag/empty-table/)

 * 2 replies
 * 2 participants
 * Last reply from: [dovydas](https://wordpress.org/support/users/dovydas/)
 * Last activity: [16 years, 6 months ago](https://wordpress.org/support/topic/check-if-table-is-empty/#post-1281221)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
