#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Oxnard, CA USA
    Posts
    3
    Rep Power
    0
    I'm a newbie and need some help. Basically, I want to count the number of names in a field to a web page. I use the following line to gather the data and store to a variable:

    $tally=mysql_query("SELECT count(*) FROM fname",$db);

    I use the next line to post it:

    print $tally;

    The output on the web page should be the number of rows counted in the field but I get:

    Resource id #2

    Any help is appreciated. Tx.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Oxnard, CA USA
    Posts
    3
    Rep Power
    0
    I solved the problem using a different approach. Here's the way I did it:

    $tally=mysql_query("SELECT * FROM fname",$db);

    echo "The total number of entries is: ".mysql_num_rows($tally)."n";

    Worked like a charm. It pays to read the past forum entries. Tx.
  4. #3
  5. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4492
    You could use count(*), but you have to use it like this.

    $tally = mysql_query("SELECT count(*) as the_count from fname",$db);
    $row = mysql_fetch_row($tally);
    echo "Total is " . $row["the_count"];

    you have to give the count(*) a name to refer to it in the php statements...

    For what you want, just a count of how many rows are in the database, how you do it is fine. but say you wanted to find out how many last names in the database start with H??

    $total = mysql_query("SELECT count(last_name) AS h_names FROM database WHERE last_name LIKE 'H%'",$db);
    $row = mysql_fetch_row($total);
    echo "Total is " . $row["h_names"];


    My syntax may be a little rusty, but you get the idea of why count(XXX) as YYY is very helpful, right??

    HTH,
    ---John Holmes...
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Oxnard, CA USA
    Posts
    3
    Rep Power
    0
    Thanks for your solution. As a matter of fact, I was planning on narrowing down the row count to specific names and your input will help me cut down on the amount of code necessary to acheive the result.

Similar Threads

  1. How do I define INT cell to be the count of other cells?
    By leethao in forum PostgreSQL Help
    Replies: 3
    Last Post: February 15th, 2004, 11:12 AM
  2. Replies: 8
    Last Post: February 8th, 2004, 12:04 AM
  3. How to wrap text in table column
    By phpfan in forum PHP Development
    Replies: 1
    Last Post: January 31st, 2004, 11:49 AM
  4. sql column join?
    By dbosz in forum MySQL Help
    Replies: 4
    Last Post: January 30th, 2004, 09:34 AM
  5. Replies: 0
    Last Post: January 3rd, 2004, 08:33 AM

IMN logo majestic logo threadwatch logo seochat tools logo