SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: counting issue

  1. #1
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    counting issue

    I was wondering... I want to count the times a certain value is in the database... I don't know the value dough...

    For instance ...

    I have a table visitors
    with
    visit_id
    firstname
    lastname
    gender
    etc...

    now I want to create a report that shows all the firstnames (but only once) and I want to show the number that a firstname exists..

    I know I can do something like
    PHP Code:
    $sql "select distinct firstname from visitors";
    $result = @mysql_query($sql);
    While(
    $row mysql_fetch_array($result))
    {
    $name $row["firstname"]
    echo 
    "firstname = $name";
    $sql2 "count("firstname") as num where firstname = '$name'";
    $result2 = ($sql2);
    echo 
    $result["num"];
    }
    ?> 
    But I was wondering if there is a better way of doing something like this...

    Greets

    Peanuts
    Last edited by peanuts; Mar 18, 2002 at 09:44.
    the neigbours (free) WIFI makes it just a little more fun

  2. #2
    SitePoint Member burchyk's Avatar
    Join Date
    Mar 2002
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your quiery should be:
    select firstname , count(firstname) from db.table group by firstname

    It will return you two columns:
    firstname(once) | times found
    Last edited by burchyk; Mar 18, 2002 at 09:57.

  3. #3
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by burchyk
    Your quiery should be:
    select firstname , count(firstname) from db.table group by firstname

    It will return you two columns:
    firstname(once) | times found
    Would that also change the way I echo the data ???

    Or is it still just

    select firstname , count(firstname) as num from db.table group by firstname

    and then

    echo $row["num"]

    ???

    thanks in advance..

    Peanuts
    the neigbours (free) WIFI makes it just a little more fun

  4. #4
    SitePoint Member burchyk's Avatar
    Join Date
    Mar 2002
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it may look like this:
    PHP Code:
    for ($i=0$i<$num_results$i++)
    {
    $row=mysql_fetch_array($result);
    print(
    $row["firstname"]." ".$row["count(firstname)"]."<br>");

    Formatting is yours

  5. #5
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    Its working thanks
    the neigbours (free) WIFI makes it just a little more fun


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
  •