SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    england
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help getting count from database

    Hi
    Im trying to get a count from a field in my database, the field is called rating, in the field rating ,they could have 4 different ansewers very good,good, bad, very bad. im trying to get the count of very good , i have written some php code but does'nt work
    here is the code i'm trying.
    PHP Code:
    <?
    $votes 
    mysql_query(" SELECT count( * ) as rating  FROM vote WHERE rating= 'Very good'  ");

        }
    if (!
    $votes) {
      exit(
    '<p>Error retrieving votes from database!<br />'.
          
    'Error: ' mysql_error(). '</p>');
    }

    while (
    $count mysql_fetch_array($votes)) {
    $rating $count['Very good'];
        
        
    ?> Votes <strong>
          <? $count $_GET['Very good'];
          echo
    "<div align=center>".$count['Very good']."</div>";
          
    ?>

  2. #2
    SitePoint Addict miggl's Avatar
    Join Date
    Feb 2007
    Location
    Los Angeles, CA
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    PHP Code:
    <?php
        $r_recordset 
    mysql_query(" SELECT COUNT(*) AS rating  FROM vote WHERE rating= 'Very good'");
        if (!
    $r_recordset) {exit('<p>Error retrieving votes from database!<br />' 'Error: ' mysql_error() . '</p>');}
        else
        {
            while (
    $r_row mysql_fetch_array($r_recordset))
            {
                
    $rating $r_row[0];
    ?>
    Votes <strong><?php print($r_row[0]); ?></strong>
    <?php
            
    }
        }
    ?>
    Celebrate Liberty, Freedom, and Rights at The Constitutionalist.

  3. #3
    SitePoint Enthusiast XRay's Avatar
    Join Date
    Aug 2006
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you use COUNT(*) in your query, you will get just one result and that will be number of votes with rating 'Very Good', which means that you don't need while loop for that, and this can't work.
    If you need to show total votes with some rating then:
    $votes = mysql_query(" SELECT count( * ) as rating FROM vote WHERE rating= 'Very good' ");

    should go... $count = mysql_fetch_assoc($votes); and that will display for example ["rating"] => 6.

  4. #4
    SitePoint Addict ruba's Avatar
    Join Date
    Apr 2005
    Location
    Amman -Jordan
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't understand these lines in ur code
    Code:
          <? $count = $_GET['Very good']; 
          echo"<div align=center>".$count['Very good']."</div>"; 
          ?>
    and
    Code:
    $rating = $count['Very good']; //where is the variable??
    I think it must be
    Code:
          <?   echo"<div align=center>".$count['rating']."</div>";   ?>
    Open Blocked website
    Open Blocked Website
    Knowledge Is Knowing That A Tomato Is A Fruit,
    Wisdom Is Not Putting It In A Fruit Salad.

  5. #5
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    england
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Hi
    thankyou
    i used miggl code it worked fine thankyou


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
  •