Php mysql count rows where condition

look at the database here ---->//www.tiikoni.com/tis/view/?id=908d940

in the subcommentid rows, value 63 is repeating maximum time itself 7 times

i want to fetch this value in numerical form means like this

maximum rows in subcommentid is 7

i hv tried this

$query=mysqli_query["SELECT * FROM table WHERE where id='63'"];
    echo mysqli_num_rows[$query];

but

but 63 is a variable so how can a server pre decide that 63 is repeating itself maximum nos

nd look at dix ques too cant get my desire result of max[like]

asked Aug 19, 2014 at 7:55

6

use select count[*] from.....

Or

use mysql function mysql_num_rows[$query_result].

answered Aug 19, 2014 at 8:01

2

Try using this query -

$query = mysqli_query["SELECT subcommentid,COUNT[*] as count FROM table GROUP BY subcommentid ORDER BY count DESC;"];
$result = mysqli_fetch_array[$query];
echo $result['subcommentid'].'
'; //subcomment id echo $result['count']; // number of rows

If you want all, store in array -

$results = array[];
while[$row = mysqli_fetch_array[$query]] {
    $results[$row['subcommentid']] = $row['count'];
}
echo "
";print_r[$results];exit;

answered Aug 19, 2014 at 8:33

TBITBI

2,7211 gold badge16 silver badges21 bronze badges

2

$query = "SELECT * FROM comments WHERE id=`7`";
$result = mysqli_query[$sql, $query];
$rows = mysqli_num_rows[$result];

answered Aug 19, 2014 at 8:01

Use mysqli_num_rows function in php to count the no.of rows. Your code should look like this

$query=mysqli_query["SELECT * FROM table WHERE where id='63'"];
    echo mysqli_num_rows[$query]; // will echo no.of rows having id 63

Hope this helps you

answered Aug 19, 2014 at 8:03

Utkarsh DixitUtkarsh Dixit

4,1973 gold badges14 silver badges36 bronze badges

2

Last update on August 19 2022 21:50:42 [UTC/GMT +8 hours]

COUNT[] function

MySQL COUNT[] function returns a count of a number of non-NULL values of a given expression.

If it does not find any matching row, it returns 0.

Syntax

COUNT[expr];

Where expr is an expression.

MySQL Version: 5.6

Example : MySQL COUNT[] function

The following MySQL statement will return the number of rows in author table.

Sample table: author

Code:

SELECT COUNT[*]
FROM author;

Sample Output:

mysql> SELECT COUNT[*]
    -> FROM author;
+----------+
| COUNT[*] |
+----------+
|       15 | 
+----------+
1 row in set [0.00 sec]

PHP script






example-aggregate-functions-and-grouping-count-function- php MySQL examples | w3resource




Counting how many authors are there in the authors table:

Number of authors

View the example in browser

JSP Script








JSP Page



Number of authors

Example : MySQL COUNT[] with logical operator

The following MySQL statement returns the number of publishers for USA and UK. The WHERE clause filters the rows for the country USA and UK. Grouping is performed on country and pub-city columns by GROUP BY and then COUNT[] counts a number of publishers for each groups.

Sample table: publisher

Code:

SELECT country,pub_city,COUNT[*]
FROM publisher
WHERE country='USA' OR country='UK' 
GROUP BY country,pub_city;

Sample Output:

mysql> SELECT country,pub_city,COUNT[*]
    -> FROM publisher
    -> WHERE country='USA' OR country='UK' GROUP BY country, pub_city;
+---------+-----------+----------+
| country | pub_city  | COUNT[*] |
+---------+-----------+----------+
| UK      | Cambridge |        1 | 
| UK      | London    |        1 | 
| USA     | Houstan   |        1 | 
| USA     | New York  |        2 | 
+---------+-----------+----------+
4 rows in set [0.00 sec]

Pictorial Presentation

PHP script






example-aggregate-functions-and-grouping-count-with-logical-operator- php MySQL examples | w3resource




Taking an account of how many authors are there in different cities of USA and UK:

CountryCityNumber of authors

Chủ Đề