SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Easiest way to get number of rows in a table?

    What is the easiest and most efficient query to get a the number of rows in table?
    Have a good day.

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2005
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To query a MySQL database with PHP.

    Code:
    $query = 'select * from blah where type="foo"';
    $result = mysql_query($query);
    $numRows = mysql_num_rows($result);

  3. #3
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah I know that way but is there a more efficient way?
    Have a good day.

  4. #4
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Darren884
    Yeah I know that way but is there a more efficient way?
    Not that I know of. The code you've been given is as efficient as it gets.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    as efficient as it gets? no way

    what you've described is to transfer all the rows (what if there are a few hundred thousand of them?) in their entirety (what if each row contains a few thousand bytes?) from the database to the scripting engine, where you use a function to count the number of rows transferred

    that's not efficient, and i don't care if it's an ethernet pipe or whatever

    how about this --
    Code:
    select count(*) as rows from thetable
    this transfers one row of minuscule size (one column) containing the answer

    substantially more efficient

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •