SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    check if table is empty

    hello

    can anyone tell me how can i check if a table is empty, is there something which can test if it is void??

    $query = "SELECT * FROM sessions WHERE (created < time()-1200)";
    $result = mysql_query($query);

    for the moment i am using this but if i try to do anything with $result it causes an error.

    Garrett

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    lalal
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $query "SELECT * FROM sessions WHERE (created < time()-1200)";
    $result mysql_query($query); 
    if(
    mysql_num_rows($result)>0)
    {
    echo 
    'do something with results';
    }
    else
    {
    echo 
    'empty result set';

    or you could use mysql_fetch_object/mysql_fetch_array and use is_object/is_array on the result,
    for example

    PHP Code:
    $query "SELECT * FROM sessions WHERE (created < time()-1200)";
    $result mysql_query($query); 
    $object=mysql_fetch_object($result);
    if(
    is_object($object))
    {
    echo 
    'do something with results';
    }
    else
    {
    echo 
    'empty set';


  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2000
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    same thing as i have already tried really and your two suggestions create the same errors that mine have been creating. ie

    "Supplied argument is not a valid MySQL result resource in"

    the two following lines are what are creating the problems because its trying to do something with $result which is nothing!!

    if(mysql_num_rows($result)>0);
    $object=mysql_fetch_object($result);

    garrett

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    lalal
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well then your query is invalid, looking at it i suspect you want

    PHP Code:
    $query "SELECT * FROM sessions WHERE (created < ".time()-1200.")"


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
  •