I want to count the number of rows in a table (using PHP and Mysql) all of the count() functions I can find only return columns, or array features, is there a function to return the number of rows? Many thanks!
Asked
Active
Viewed 98 times
0
-
You mean this ? http://in1.php.net/manual/en/function.mysql-num-rows.php – Shankar Narayana Damodaran Feb 09 '14 at 15:52
-
Have you tried my answer yet? – user2397282 Feb 09 '14 at 16:50
-
why did you decide that [count()](https://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_count) doesn't return number of rows? – Alex Kulinkovich Feb 10 '14 at 08:06
-
also see [this question](http://stackoverflow.com/questions/5060366/mysql-fastest-way-to-count-number-of-rows). It should explain why better and smarter use mysql count(). – Alex Kulinkovich Feb 10 '14 at 08:16
4 Answers
0
you can count the number of rows returned by a query. note that this function is deprecated... it has options on the docs page here.

rkstar
- 1,178
- 14
- 27
0
You need to use mysqli_num_rows() :
$con=mysqli_connect("host","name","pass","database");
$sql="SELECT * FROM table_name";
$result=mysqli_query($con, $sql);
$rowcount=mysqli_num_rows($result);
$rowcount now stores the number of rows.

user2397282
- 3,798
- 15
- 48
- 94
0
$count = mysql_num_rows($query);
would return the number of rows selected by the query, make sure you are not using count()
in the query itself.

TLama
- 75,147
- 17
- 214
- 392
-1
Depending if you are using mysql, mysqli or PDO query.
if mysql_query rkstar told you how to do it :
if mysqli_query : http://php.net/manual/fr/mysqli-stmt.num-rows.php
if PDO : do another query ! $rowCount = $db->query("SELECT FOUND_ROWS()")->fetchColumn();

aviel
- 178
- 9