• Resolved dovydas

    (@dovydas)


    Hi.

    I want to check if table is empty and then I want to insert some initial data. According to https://www.mail-archive.com/[email protected]/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

    (@michaelh)

    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

    (@dovydas)

    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.