Php mysql count rows where condition

look at the database here ---->http://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

Php mysql count rows where condition

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:

query('SELECT COUNT(*) FROM author') as $row) { echo ""; echo ""; echo ""; } ?>
Number of authors
" . $row['COUNT(*)'] . "

View the example in browser

JSP Script

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>




JSP Page


<%
try {
Class.forName("com.MySQL.jdbc.Driver").newInstance();
String Host = "jdbc:MySQL://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data = "SELECT COUNT(*) FROM author";
rs = statement.executeQuery(Data);
%>

<%
while (rs.next()) {
%>

<%   }    %>
Number of authors
<%=rs.getString("COUNT(*)")%>
<% rs.close(); statement.close(); connection.close(); } catch (Exception ex) { out.println("Can’t connect to database."); } %>

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 mysql count rows where condition

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:

query('SELECT country,pub_city,COUNT(*) FROM publisher WHERE country="USA" OR country="UK" GROUP BY country,pub_city;') as $row) { echo ""; echo ""; echo ""; echo ""; echo ""; } ?>
CountryCityNumber of authors
" . $row['country'] . "" . $row['pub_city'] . "" . $row['COUNT(*)'] . "

View the example in browser

MySQL COUNT() using multiple tables

The following MySQL statement retrieves those rows from publisher table whose 'pub_id' in publisher table match the 'pub_id' in 'book_mast' table.

A grouping operation is performed on pub_id column of publisher table by GROUP BY and then number of times pub_id exists in publisher table is counted by COUNT().

Sample table : book_mast

Sample table: publisher

Code:

SELECT publisher.pub_name,COUNT(*)
FROM publisher,book_mast
WHERE publisher.pub_id=book_mast.pub_id
GROUP BY publisher.pub_id;

Sample Output:

mysql> SELECT publisher.pub_name,COUNT(*)
    -> FROM publisher,book_mast
    -> WHERE publisher.pub_id=book_mast.pub_id
    -> GROUP BY publisher.pub_id;
+------------------------------+----------+
| pub_name                     | COUNT(*) |
+------------------------------+----------+
| Jex Max Publication          |        2 | 
| BPP Publication              |        2 | 
| New Harrold Publication      |        2 | 
| Ultra Press Inc.             |        2 | 
| Mountain Publication         |        2 | 
| Summer Night Publication     |        2 | 
| Pieterson Grp. of Publishers |        2 | 
| Novel Publisher Ltd.         |        2 | 
+------------------------------+----------+
8 rows in set (0.00 sec)

PHP script






example-aggregate-functions-and-grouping-count-with-more-tables- php MySQL examples | w3resource




Displaying the name of the publisher and their frequency in publisher table, whose publisher id present in both publisher and book_mast tables:

query('SELECT publisher.pub_name,COUNT(*) FROM publisher,book_mast WHERE publisher.pub_id=book_mast.pub_id GROUP BY publisher.pub_id') as $row) { echo ""; echo ""; echo ""; echo ""; } ?>
PublisherFrequency
" . $row['pub_name'] . "" . $row['COUNT(*)'] . "

View the example in browser

Online Practice Editor:

Previous: BIT_XOR()
Next: COUNT() with group by

How do I count the number of rows in MySQL PHP?

We can get the total number of rows in a table by using the MySQL mysqli_num_rows() function. Syntax: mysqli_num_rows( result ); The result is to specify the result set identifier returned by mysqli_query() function.

How do I count the number of rows returned by a query in MySQL?

MySQL COUNT() Function The COUNT() function returns the number of records returned by a select query.

How is it possible to know the number of rows returned in the result set in PHP?

The mysqli_num_rows() function returns the number of rows in a result set.

How do I count the number of data in MySQL?

How to use the COUNT function in MySQL.
SELECT * FROM count_num; Run..
SELECT COUNT(*) FROM numbers; Run..
SELECT COUNT(*) FROM numbers. WHERE val = 5; Run..
SELECT COUNT(val) FROM numbers; Run..
SELECT COUNT(DISTINCT val) FROM numbers; Run..