0

Given Database Table

Database Example

Expected Output

  1. No of Occurrence of A
  2. No of Occurrence of B
  3. No of Occurrence of C
  4. ...

using PHP

1 Answers1

3

Actually this is something you would solve much more easily using SQL, not PHP:

SELECT
  Agent,
  COUNT(Agent)
FROM
  yourtable
GROUP BY
  Agent

Results:

Agent   COUNT(Agent)
--------------------
A       4 
B       4
C       2

Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=86482e0d543a21ed7b0a22d7ab498e7e

This is called an "aggregate query", where you group the results by a specific column and the form a function on those results.

More info about COUNT and GROUP BY can be found in the MySQL documentation, and numerous SQL tutorials etc around the web.

ADyson
  • 57,178
  • 14
  • 51
  • 63