Sreejith
Sreejith

Reputation: 19

To get the number of counts from database using php

In my database a table contains different values such as

2,2,2,2,2,2,2,2,16,16,16 .

I need to get the count of each digits using PHP codes.

For example count of'2'=8 and count of'16' =3 so on.

I already tried the count function but it has not worked.

Upvotes: 1

Views: 54

Answers (3)

Craftein
Craftein

Reputation: 762

 SELECT
    DISTINCT column_name,
    (SELECT COUNT(column_name) FROM table_name WHERE column_name = tn.column_name) AS cnt
 FROM table_name tn;

This outputs

column_name | cnt
2           | 8
16          | 3

Upvotes: 0

xzag
xzag

Reputation: 604

SELECT column_name, COUNT(*) AS cnt FROM table_name GROUP BY column_name;

Upvotes: 0

Mr. Alien
Mr. Alien

Reputation: 157314

Fetch the row from the database, now I assume you can do this, then, use

$store = explode(',', $row['column']; //explode the values, creates array

$fetch_dups = array_count_values($store); //duplicates

$get_count = count($fetch_dups); //returns count for each Value

print_r($get_count);

Upvotes: 1

Related Questions