SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast GirlsOnBikes's Avatar
    Join Date
    May 2002
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to check if table is empty

    Hello All,
    I was wondering if there is a MySQL command to check if a particular table is emtpy. I have the following code snippet here:
    PHP Code:
    if (TABLE_IS_EMTPY) {
    echo 
    "DO_SOMETHING_HERE";
    } else {
    for (
    $k=0$row mysql_fetch_row($model_result); $k++) {
        echo 
    "DO_SOMETHING_ELSE";
    }

    I've searched through the MySQL documentation, but didn't find anything.
    Any ideas?
    Thanks

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i don't know whether you want to check if the table is empty, or if a certain query returned no rows.

    to check if the whole table has any rows:

    PHP Code:
    <?php

    $rows 
    mysql_result(mysql_query('SELECT COUNT(*) FROM table'), 0);

    if (!
    $rows) { /* Table is empty */ }

    ?>
    to check if a certain query (with a WHERE etc.) returned no rows:

    PHP Code:
    <?php

    if (!mysql_num_rows($result))
    {
        
    // No rows
    }
    else
    {
        
    // Fetch the rows
    }

    ?>
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    SitePoint Enthusiast GirlsOnBikes's Avatar
    Join Date
    May 2002
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DOETH.
    Total brain lapse. Didn't realize you could check the num_rows just from the query handle
    Duh.
    Thanks for the reminder!!
    I appreciate it.

  4. #4
    SitePoint Enthusiast Traduim's Avatar
    Join Date
    Feb 2003
    Location
    Catalonia
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please note that "SELECT COUNT(*) FROM table" will ALWAYS return one record. If the table is empty, it will return 0.

    So...
    PHP Code:
    <?
    $rows 
    mysql_result(mysql_query('SELECT COUNT(*) FROM table'), 0);
    ?>
    $rows will NEVER EVER be indefined.
    Josep Tarrés,
    traducción
    traductor

  5. #5
    SitePoint Enthusiast GirlsOnBikes's Avatar
    Join Date
    May 2002
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just used the following, worked great:

    PHP Code:
    $model_query "SELECT model FROM ".$table_array[$j]." ORDER BY model";
      
    $model_result mysql_query($model_query,$db);
      if (!
    mysql_num_rows($model_result)) {
       
    // Do Something Here  
       
    } else {
       
    // Do Something Else
      


  6. #6
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Traduim
    Please note that "SELECT COUNT(*) FROM table" will ALWAYS return one record. If the table is empty, it will return 0.

    So...
    PHP Code:
    <? 
    $rows 
    mysql_result(mysql_query('SELECT COUNT(*) FROM table'), 0); 
    ?>
    $rows will NEVER EVER be indefined.
    However, ( ! $rows ) evaluates to false if $rows is 0 anyway.

    Matt.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •