SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: select count(*)

  1. #1
    SitePoint Addict
    Join Date
    Mar 2001
    Location
    the windy city
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    select count(*)

    i'd like to count a total number of rows in a db.

    i've browsed teh net and see that i shoud use something like
    PHP Code:
    "select count(*) from navigation" 
    but what do i do with it then?

    the thing is that sometimes the records id numbers get out of order, so i actually need to count the number ofo existing rows, not determine what the highest row id is for "navigation" table.
    Goals are dreams with deadlines
    -------------------------------------
    Nimlok Trade Show Displays Booths and Exhibits

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    43 03' 16" N :::: 77 36' 03" W
    Posts
    183
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could use the php function mysql_num_rows() as well, but i don't know what is faster.
    There's no place like 127.0.0.1

  3. #3
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by valeria_vi
    i'd like to count a total number of rows in a db.

    i've browsed teh net and see that i shoud use something like
    PHP Code:
    "select count(*) from navigation" 
    but what do i do with it then?

    PHP Code:
    $sql "select count(*) as my_count from navigation";
    $result mysql_query($sql);
    $row mysql_fetch_array($result);
    $my_count $row['my_count'];
    mysql_free_result($result);

    echo 
    $my_count
    Quote Originally Posted by valeria_vi
    the thing is that sometimes the records id numbers get out of order, so i actually need to count the number ofo existing rows, not determine what the highest row id is for "navigation" table.
    Count(*) will do that
    My name is Steve, and I'm a super-villian.

  4. #4
    SitePoint Addict
    Join Date
    Mar 2001
    Location
    the windy city
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lieut_data,
    that looks like an awful lot of code for one simple thing. (i could see how i could comnbine a couple of rows into 1, but still)

    it worked. thanks!

    while i waited for replies in this thread, i countinued digging things up and came across the following solution:
    PHP Code:
    $count_rows = @mysql_query("select count(*) from navigation");
    $num_rows mysql_result($count_rows,0);
    echo 
    $num_rows
    it also worked, but I am not sure how. i tried looking up mysql_result function, but its description made no sence to me.
    could you please explain?
    Goals are dreams with deadlines
    -------------------------------------
    Nimlok Trade Show Displays Booths and Exhibits

  5. #5
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by valeria_vi
    lieut_data,
    that looks like an awful lot of code for one simple thing. (i could see how i could comnbine a couple of rows into 1, but still)
    it worked. thanks!
    Yeah, as you found out below, it can be condensed into one line, but sometimes posting 'full' code helps other readers to grasp the concept (i.e. rather than posting a single-liner which does the job, and confuses everyone )

    Quote Originally Posted by valeria_vi
    while i waited for replies in this thread, i countinued digging things up and came across the following solution:
    PHP Code:
    $count_rows = @mysql_query("select count(*) from navigation");
    $num_rows mysql_result($count_rows,0);
    echo 
    $num_rows
    it also worked, but I am not sure how. i tried looking up mysql_result function, but its description made no sence to me.
    could you please explain?
    Yup -- mysql_result just grabs the xth returned column value -- it's the one-liner solution to:

    PHP Code:
    $row mysql_fetch_array($result);
    $count $row['count']; 
    My name is Steve, and I'm a super-villian.


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
  •