I have a query like SELECT * table from etc order by something LIMIT 10
but in the same time I also want to know the TOTAL row results in the table. Do I need to do another sql?
Asked
Active
Viewed 106 times
-1

Your Common Sense
- 156,878
- 40
- 214
- 345

user3318525
- 9
- 3
-
1You can do that using PHP by calling [mysqli_num_rows()](http://pk1.php.net/manual/en/mysqli-result.num-rows.php) – Aziz Shaikh Feb 19 '14 at 05:32
-
@AzizShaikh I want to know the total. – user3318525 Feb 19 '14 at 05:35
-
'total' as in 'count' of the rows returned by your sql query OR 'sum' of values from the specific column? – Aziz Shaikh Feb 19 '14 at 05:36
-
You could use COUNT(*) aggregate function? – Edper Feb 19 '14 at 05:37
-
@Edper count and select can be use together? – user3318525 Feb 19 '14 at 05:38
-
@user3318525: by "total", you mean number of rows matching a filter clause independent of your limit...? – bob-the-destroyer Feb 19 '14 at 05:39
-
@user3318525 Yes, like `SELECT COUNT(*) as TotalRows FROM etc`. But if you are using php you could just simply use `SELECT * FROM etc` and then use `mysql_num_rows($query)`' – Edper Feb 19 '14 at 05:43
3 Answers
0
If you want to see total rows return by the query you use mysqli_num_rows($sql)
$sql = mysqli_query($con,"SELECT * from etc order by something LIMIT 10");
$total_rows = @mysqli_num_rows($sql);
Edit :- If you want total of specifc coloumn say 'id' you can do like this :-
$total = 0;
while($result = mysqli_fetch_assoc($sql))
{
$total = $total + $result['id'];
}
echo $total;

Rakesh Shetty
- 4,548
- 7
- 40
- 79
0
I had the same question, I had to do another sql... Here is what I used to count the numbers of users (userid>16 was due to sueprusers and deleted accounts):
SELECT
COUNT(*) AS "Number of users"
FROM
Users
where userid>16

user3223048
- 163
- 7
-2
If you are using mysql use this:
$conn = mysql_connect('localhost', 'usename', 'password');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('database_name');
mysql_query('SELECT * table from etc order by something LIMIT 10');
echo mysql_affected_rows();

Karim Lahlou
- 168
- 5
-
-
-
@Karim Lahlou: please don't encourage the use of the old `mysql` extension and the "`die` on failed connections" routine. – bob-the-destroyer Feb 19 '14 at 05:44