Easiest way to count rows

Discussion in 'PHP' started by x0x, Jan 23, 2010.

  1. #1
    $pull = $DB->query("SELECT * FROM table WHERE id > 0", __FILE__, __LINE__);
    $count = $DB->num_rows($pull);

    is that the fastest way to count rows and get the value to a var?
     
    x0x, Jan 23, 2010 IP
  2. aquilax

    aquilax Member

    Messages:
    126
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #2
    It will be faster if you can get rid of "WHERE id>0"
     
    aquilax, Jan 23, 2010 IP
  3. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Ah, ok. Would it be possible to count them with SELECT COUNT(id) * blalba and get it in a var?
     
    x0x, Jan 23, 2010 IP
  4. aquilax

    aquilax Member

    Messages:
    126
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #4
    Yes but without the "*"

    SELECT count(*) FROM bla_bla;

    should work
     
    aquilax, Jan 23, 2010 IP
  5. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #5
    How would I get it to a var?

    var_dump returns this


    resource(22) of type (mysql result) resource(26) of type (mysql result) resource(30) of type (mysql result) resource(34) of type (mysql result) resource(38) of type (mysql result)
     
    x0x, Jan 23, 2010 IP
  6. aquilax

    aquilax Member

    Messages:
    126
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #6
    
    $sql = "SELECT count(*) FROM table";
    $query = mysql_query($sql);
    list($rows) = mysql_fetch_row($query)
    
    Code (markup):
    You'll have the count in $rows
     
    aquilax, Jan 23, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    or...

    <?php
    $sql = mysql_query("SELECT * FROM table");
    $num = mysql_num_rows($sql);
    echo $num. "Rows";
    ?>
    PHP:
     
    danx10, Jan 23, 2010 IP