January 9, 2011
Count especific table rows in mysql
Question by NORM
I want to count how many rows I have in a mysql table and display it in html.
For example:
There is: Item 1, Item 2, Item 3, Item 4, Item 5, Item 6
If Item 1 and Item 2 are filled, I want a html code to say – 2 items in row
any ideas?
Answer by Starx
IF its total rows you are after, then as Nishant mentioned above you could do something like this
$query = "SELECT COUNT(*) FROM mytable WHERE myfield='myvalue'";
I am counting all the ids, because it will reduce the time taken to calculate the total instead of ‘*’ and
Thanks to Nico, count(*) and count(column) are not so different considering performance. Here is a comparision
Use a where condition to narrow your total as you require.