You are here: Re: How many rows in a table « PHP Programming Language « IT news, forums, messages
Re: How many rows in a table

Posted by Jerry Stuckle on 12/31/06 20:57

GT wrote:
> bill wrote:
>
>> Andy Hassall wrote:
>>
>>> On Sat, 30 Dec 2006 17:04:58 -0500, bill <nobody@spamcop.net> wrote:
>>>
>>>> I am sure there must be an easy way to determine the number of rows
>>>> in a table, but I can't find it.
>>>>
>>>
>>> select count(*) from your_table
>>>
>>
>> $total_rows = mysql_query ("SELECT COUNT(*) FROM Kennel");
>>
>> gives me a resource, not the count.
>
>
> I guess it would. If you were to do it that way:
>
> $result = mysql_query ("SELECT COUNT(*) FROM Kennel");
> $array = mysql_fetch_array($result);
> $rows = $array[0];
>

Yes, this would be the correct way to do it.

> (or similar - I've not tested, but something akin to that will work)
>
>> Fine, so I use:
>> $total_rows = mysql_query ("SELECT COUNT(*) FROM Kennel");
>> echo "Rows in table: " . mysql_num_rows ($total_rows) . "<br />";
>>
>> but that returns 1, and there are 4 rows in the table.
>
>
> No no!
> $result = mysql_query ("SELECT * FROM Kennel");
> $rows = mysql_num_rows ($result);
>

Definitely not! What if the table has 10M rows? You're asking they all
be returned to the program so they can be counted.

Let MySQL do the counting. That's what COUNT(*) does.

> HTH



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация