Jump to content

statistics- counting mysql rows and display a number


phpmikey

Recommended Posts

hi

 

I would like to be able produce statistics on my web page by counting the numbers of rows in my sql database.

 

for example.

 

within my database i have 10 defects in my table called "defects". 9 defects are major, 1 defect is a minor.

 

is there a way to extract this data in numeric form rather than printing the values as they come? 

 

i would like statistics in my web page to tell me how many defects are major and how many are minor.

 

major defects: 9

minor defects: 1

 

something like: echo count FROM defects WHERE name=major  ::) eventually i would like to be able to produce graphs but i think that is a bit beyond me but simple tables showing the statistics would be very useful for a start.

 

thank you for any help you can offer 

hi

 

I would like to be able produce statistics on my web page by counting the numbers of rows in my sql database.

 

for example.

 

within my database i have 10 defects in my table called "defects". 9 defects are major, 1 defect is a minor.

 

is there a way to extract this data in numeric form rather than printing the values as they come? 

 

i would like statistics in my web page to tell me how many defects are major and how many are minor.

 

major defects: 9

minor defects: 1

 

something like: echo count FROM defects WHERE name=major  ::) eventually i would like to be able to produce graphs but i think that is a bit beyond me but simple tables showing the statistics would be very useful for a start.

 

thank you for any help you can offer

 

I'm not sure exactly how your table is designed. But from what I gather, you could do single query to get the data you need. Try something like:

 

$sql = "SELECT name, count(*) FROM defects GROUP BY name";

 

This query would result with each name, and the count of that name. If that doesn't help. Try posting a copy of your table and I may be able to help you better.

 

thanks for your reply.

 

the database table structure is

id

severity

description

1

major

description example

2

major

description example

3

minor

description example

 

so the php code i would like to have would count the number of rows with the "severity" name 'major' and post 2, and count the number of rows with 'minor' and display a 1. then i could echo these numbers into a statistics table like such:

 

major: 2

minor: 1

 

i hope this explains.

 

would the example code you posted achieve  this?

 

thanks for your reply.

 

the database table structure is

id

severity

description

1

major

description example

2

major

description example

3

minor

description example

 

so the php code i would like to have would count the number of rows with the "severity" name 'major' and post 2, and count the number of rows with 'minor' and display a 1. then i could echo these numbers into a statistics table like such:

 

major: 2

minor: 1

 

i hope this explains.

 

would the example code you posted achieve  this?

 

Let me give you a little sample code based off what you've given. This isn't the full code. You'll need statements to connect to the database. But this will display the information in the format you're looking for.

 

$sql = "SELECT severity, count(*) FROM defects GROUP BY severity";

$result = mysql_query($sql);

 

while($row = mysql_fetch_array($result, MYSQL_NUM))

{

 

echo "$row[0]: $row[1]";

 

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.