SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Mysql Group By

  1. #1
    SitePoint Evangelist klik's Avatar
    Join Date
    Jan 2005
    Location
    Scotland, UK
    Posts
    547
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mysql Group By

    PHP Code:
                  <?php
            $section_data 
    mysql_query("SELECT fav_section, COUNT(fav_section) AS most_popular FROM cms_users GROUP BY fav_section ORDER BY most_popular DESC LIMIT 1");
            
            
    $row mysql_fetch_assoc($section_data);
                    
                    echo 
    '<font color ="#FF3300">' $row[fav_section] . ' - ' $row[most_popular] . ' votes</font>'

            
    ?>
    What I want is the most popular section to be echoed and the number of times it has been selected. The quantity works but no section name is displayed.

    Can anyone see anything wrong with this code?
    (I posted this in the PHP forum with no response, and decided this would be a better suited place to ask)

  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)
    you have to put quotes around the names of the array elements:
    PHP Code:
    echo '<font color ="#FF3300">' $row['fav_section'] . ' - ' $row['most_popular'] . ' votes</font>'

  3. #3
    SitePoint Evangelist klik's Avatar
    Join Date
    Jan 2005
    Location
    Scotland, UK
    Posts
    547
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still no change, I'm afraid.

  4. #4
    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)
    try these two things separately:

    1) make it LIMIT 1,1
    2) change $row['fav_section'] to htmlspecialchars($row['fav_section'])

    if 1 fixes the problem, then your favorite section has no name.
    if 2 fixes the problem, then, well, that fixes it.

  5. #5
    SitePoint Evangelist klik's Avatar
    Join Date
    Jan 2005
    Location
    Scotland, UK
    Posts
    547
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first one fixed it, 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
  •