SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: mysql_num_rows

  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Wolverhampton, UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql_num_rows

    ok, i writing a nwes script and want to find out how many comments there are on a particluar piece of news:

    Code:
    function comments($id){
               $result = mysql_query("SELECT * FROM comments WHERE comments.nid = " . $id);
               $comments = mysql_num_rows($result);
    
               return "<a href=comments.php?post=" . $id . ">" . $comments . ($comments == 1 ? "Comment..." : "Comments...") . "</a><br>";
      }
    is me simple little function for this where $id is the number of the news post in question...

    however, when there are no comments on this piece of news, num_rows throws a wobbly and prints an error message so screwin' up me page, pah

    is there anyway of solving this or getting round this, could i use SELECT count(*) but not sure what that returns and how to print it to the page, ie there are count(*) comments...

    please help,
    beachball

  2. #2
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use:

    PHP Code:
    if ($comments !== 0) {

    return 
    "<a href=comments.php?post=" $id ">" $comments . ($comments == "Comment..." "Comments...") . "</a><br>";

    } else {

    return 
    "<p>No comments</p>";


    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Wolverhampton, UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that not the prob really...

    i want it to link o comments page even if there aren't ne as this is where u can add comments as will be able to see other comments and then add your own after them...

    however:

    Warning: Supplied argument is not a valid MySQL result resource on the $comments = ... line

    is what i get and this make me page look ugly

    gotta try + get rid of it, and it's because of summin' to do with first two lines of the comments() function..

    beachball

  4. #4
    SitePoint Addict jamesbond's Avatar
    Join Date
    Feb 2001
    Location
    The Netherlands
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this what you're looking for ?

    if ($result) { $comments = mysql_num_rows($result); }

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Wolverhampton, UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ooooo, now that looks promisin'...

    i'll try it out,
    beachball

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Wolverhampton, UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    fan(-diddly-do-da-)tastic...

    hehe, edited me other scripts to use that technique now instead of havin' to put up with ugly error messages if there happens to be nothing returned by my query

    beachball


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
  •