SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    COUNT (Database Query) Question

    Consider the following script:

    PHP Code:
    $result mysql_result(mysql_query("SELECT COUNT(GTA.ID)
    FROM g_topics TOP
    LEFT JOIN g_topics_articles GTA ON GTA.ID = TOP.Name
    WHERE TOP.Parent = '
    $MyURL' AND TOP.Site = '$MySiteID'"),0);

    switch(
    $result)
    {
     case 
    1:
     
    $Children 1;
     break;
     case 
    0:
     
    $Children 0;
     break;
     default:
     
    $Children 2;
     break;

    It correctly displays 0 if there are no results, 1 if there's one row match and 2 for two or more matches. I'd like to modify it so that the default choice displays the exact number of rows. So if there are two rows, it would display "2", if there are three rows, it displays "3", etc.

    I changed the default setting to this:

    PHP Code:
     default:
     
    $Children $result;
     break; 
    ...but it still displays "2", even if there are three matching rows.

    I tried following the example at http://www.tizag.com/mysqlTutorial/mysqlcount.php, but that's designed for displaying the names of rows, not the number of rows.

    Thanks for any tips.

  2. #2
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    i don't see any problem with the code you provided.

    i assume that you've tried the query outside of PHP with values that do generate numbers of 3 or higher to verify that your data is not the problem?

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by longneck View Post
    i don't see any problem with the code you provided.

    i assume that you've tried the query outside of PHP with values that do generate numbers of 3 or higher to verify that your data is not the problem?
    Good tip; I discovered that I the value I'm testing for had three children in one table but not the other. Thanks.


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
  •