# \[PHP\]Get number of table entries

**URL:** <https://forum.kirupa.com/t/php-get-number-of-table-entries/242710>\
**Category:** programming\
**Created:** [October 29, 2007, 6:08pm UTC](https://forum.kirupa.com/t/php-get-number-of-table-entries/242710 "2007-10-29T18:08:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bardic](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bardic/32/3708_2.png) [@bardic](https://forum.kirupa.com/u/bardic)\
**Post date:** [October 29, 2007, 6:08pm UTC](https://forum.kirupa.com/t/php-get-number-of-table-entries/242710/1 "2007-10-29T18:08:14Z")

</div>

I am trying to loop through a table and get the length of it so I can ensure that on the final iteration of the loop, it does what I want. I tried used $r=mysql\_fetch\_array and count($r) but it gives me 14 when I only have for entries in the table.

Here’s my php. As I said there are 4 rows in the table that I am pulling this from, but it isn’t counting them properly. Is there something I am missing or do I need to create a loop first to count the rows in the table?

```php

<?php
                $count =0;
                $loop = 0;
                $q=mysql_query("select * from users");
                while($row = mysql_fetch_array($q)){
                    if($count==0){
                        echo "<div class='lawyerRow'>";
                        echo"<span class='lawyerBubble'><img src='images/lawyer.png' /><h4>".$row["fname"]." ".$row["lname"]."</h4><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p></span>";
                        $count++;
                        if(count($row) == $loop){
                            echo"</div>";
                        }
                    }elseif($count==3){
                        echo "</div>";
                        if(count($row) != $loop){
                            echo "<div class='lawyerRow'>";
                            echo"<span class='lawyerBubble'><img src='images/lawyer.png' /><h4>".$row["fname"]." ".$row["lname"]."</h4><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p></span>";
                            $count=1;    
                        }                    
                    }else{
                        echo"<span class='lawyerBubble'><img src='images/lawyer.png' /><h4>".$row["fname"]." ".$row["lname"]."</h4><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p></span>";
                        $count++;                        
                        if(count($row) == $loop){
                            echo"</div>";
                        }
                    }
                    
                    echo "loop = ".$loop;
                    echo "count = ". count($row);
                    $loop++;
                }
            ?>

```

Thanks for the help in advanced.
