Results 1 to 11 of 11

Thread: find number of records [resolved]

  1. #1

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090

    find number of records [resolved]

    How do you find the number of records in a DB using MySQL?
    Last edited by Acidic; Feb 29th, 2004 at 11:44 AM.
    Have I helped you? Please Rate my posts.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    I'm assuming that as you posted this in the PHP forum you are using mysql and PHP. If that is the case then the:

    mysql_num_rows() function will do the trick on a previously run select query:
    PHP Code:
    $result mysql_query ("SELECT * FROM mytable;"$link);

    echo (
    "Number of rows in table: " mysql_num_rows($result)); 
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    You can also do a query to count it itself without calling a PHP function to get it.

    PHP Code:
    <?
      $qry = mysql_query("SELECT COUNT(*) FROM table WHERE conditions='value'");
      $arr = mysql_fetch_array($qry);
      echo "number of records: $arr[0]\n";
    ?>
    Like Archer? Check out some Sterling Archer quotes.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Which is much much faster.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by kows
    You can also do a query to count it itself without calling a PHP function to get it.

    PHP Code:
    <?
      $qry = mysql_query("SELECT COUNT(*) FROM table WHERE conditions='value'");
      $arr = mysql_fetch_array($qry);
      echo "number of records: $arr[0]\n";
    ?>
    I know it's not what you meant, but mysql_query() and mysql_fetch_array() are both PHP functions.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by CornedBee
    Which is much much faster.
    I'm not doubting you are calling you wrong, but where/how did you learn this?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    I'm guessing that for PHP to count your query it's going to have to requery your query and add the MySQL COUNT() function to it, or it could loop through the query you give it and increment the count every loop through..

    Using MySQL's internal COUNT() function just runs through the table once and counts every record.. and since it's an internal function it will run faster than PHP having to count the query.

    Anyways, that's just my theory.
    Like Archer? Check out some Sterling Archer quotes.

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    Originally posted by kows
    I'm guessing that for PHP to count your query it's going to have to requery your query and add the MySQL COUNT() function to it, or it could loop through the query you give it and increment the count every loop through..

    Using MySQL's internal COUNT() function just runs through the table once and counts every record.. and since it's an internal function it will run faster than PHP having to count the query.

    Anyways, that's just my theory.
    mysql always returns the number of records in a query. PHP need just query that.

    As for which method is quicker - I'm not sure.

    With my method mysql has to create a table containing all the records and fields and store it in memory.

    In your method only the only record returned is one which contains the number of records in the table. So I guess that would mean that it is quicker.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Spot on visualAd. The lack of actual records returned makes for much space and speed savings. ESPECIALLY if the database is on a different server than the script (uncommon).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Well it would be best to use the PHP count function if your going to be needing all the records anyway. However if your only interested in how many it'd be better to use MySQL count(primary key). That probably went with out saying but I thought I'd stop the lot of you just deciding to stick with one of these for all situations .
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  11. #11

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    OK, thanks guys.

    I think I'm still too new to PHP/MySQL to comprehend what is being said. For me it works, I know I shouldn't think like that, but I do.
    Have I helped you? Please Rate my posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width