1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Mysql count or PHP count

Discussion in 'Databases' started by whiteSensation, Apr 18, 2014.

  1. #1
    What is better mysql count or PHP num rows?
     
    Solved! View solution.
    whiteSensation, Apr 18, 2014 IP
  2. #2
    When you COUNT(*) it takes in count column indexes, so it will be the best result. Mysql with MyISAM engine actually stores row count, it doensn't count all rows each time you try to count all rows. (based on primary key's column)

    Using PHP to count rows is not very smart, because you have to send data from mysql to php. Why do it when you can achieve the same on the mysql side?

    If the COUNT(*) is slow, you should run EXPLAIN on the query, and check if indexes are really used, and where should they be adde_
     
    Marko Platanic, Apr 18, 2014 IP
    nixonm likes this.
  3. junedc

    junedc Active Member

    Messages:
    175
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Agree. PHP in no way can count the db records. Somehow it will be using the mysql functions or sql to count behind the scene
     
    junedc, Apr 19, 2014 IP
  4. mymindrules

    mymindrules Greenhorn

    Messages:
    76
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    23
    #4
    You can use MySQL_num_rows if you want to count numbe of rows in the php still you can use count(*) and store it in the alias and read the variable from the array in order to get count , but according to e MySQL_num_rows is th best way
     
    mymindrules, Apr 22, 2014 IP
    seotraining likes this.
  5. benaya

    benaya Well-Known Member

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    131
    Digital Goods:
    1
    #5
    Always use mysql count based on the primary key,
     
    benaya, Jun 8, 2014 IP
  6. chrislim2888

    chrislim2888 Active Member

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    I also agree with mysql count that will provide you better response than PHP count.
     
    chrislim2888, Jun 25, 2014 IP
  7. toma_sum41

    toma_sum41 Member

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #7
    Mysql count function , for sure !
     
    toma_sum41, Jul 28, 2014 IP