Welcome to Dream.In.Code
Become a PHP Expert!

Join 149,849 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,345 people online right now. Registration is fast and FREE... Join Now!




Calculating an average of mysql row data

2 Pages V  1 2 >  
Reply to this topicStart new topic

Calculating an average of mysql row data

icowpar
15 Feb, 2007 - 11:21 AM
Post #1

New D.I.C Head
*

Joined: 15 Feb, 2007
Posts: 10


My Contributions
I'm fairly new to PHP and MYSQL, and I'm trying to get some code to work, and it isn't. I'm trying to calculate the average value of x amount of row data, where both the # of rows and amount of data will be constantly increasing.

The data I have in a mysql db contains a field, Overall, which relates to the overall score that a given item received.

The # of items will continue to grow and, as such, so will the number of cells that contain data for that field.

There are also multiple items under the same manufacturer, and I want the average to be calculated for each manufacturer.

My attempt at the code goes something like this

CODE
$query = SELECT SUM(Overall) FROM <table name> WHERE Manufacturer = <value>;
$result = @mysql_query ($sum);

$query = SELECT COUNT(Overall) FROM <table name> WHERE Manufacturer = <value>;
$result = @mysql_query ($total);

$average = $total / $sum;

echo $average;


Please help!


[mod edit] added code tags.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Calculating An Average Of Mysql Row Data
15 Feb, 2007 - 11:33 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
How about
SQL
SELECT AVG(Overall) FROM MyTable

User is online!Profile CardPM
+Quote Post

icowpar
RE: Calculating An Average Of Mysql Row Data
15 Feb, 2007 - 11:52 AM
Post #3

New D.I.C Head
*

Joined: 15 Feb, 2007
Posts: 10


My Contributions
I tried that. Here is my code. Nothing output.

CODE
<?php

require_once ('mysql_connect.php'); // Connect to the db.

$query = "SELECT AVG(Overall) FROM Master_Beer_Table WHERE Manufacturer = 'Boston Beer Company'";
$result = @mysql_query ($average);

echo $average;

?>


[mod edit] Added code tags. Please use them in the future.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Calculating An Average Of Mysql Row Data
15 Feb, 2007 - 11:55 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
What data type is field Overall?
User is online!Profile CardPM
+Quote Post

icowpar
RE: Calculating An Average Of Mysql Row Data
15 Feb, 2007 - 11:58 AM
Post #5

New D.I.C Head
*

Joined: 15 Feb, 2007
Posts: 10


My Contributions
smallint
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Calculating An Average Of Mysql Row Data
15 Feb, 2007 - 12:07 PM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
SQL
SELECT AVG(Overall) FROM Master_Beer_Table WHERE Manufacturer = 'Boston Beer Company' GROUP BY Manufacturer

User is online!Profile CardPM
+Quote Post

icowpar
RE: Calculating An Average Of Mysql Row Data
15 Feb, 2007 - 12:20 PM
Post #7

New D.I.C Head
*

Joined: 15 Feb, 2007
Posts: 10


My Contributions
That returned "Resource id #3"

CODE
<?php

require_once ('mysql_connect.php'); // Connect to the db.

$query = "SELECT AVG(Overall) FROM Master_Beer_Table WHERE Manufacturer = 'Boston Beer Company' GROUP BY Manufacturer";
$result = @mysql_query ($query);

echo $result;

mysql_free_result ($result); // Free up the resources.    

mysql_close(); // Close the database connection.
?>


[mod edit]Please use code tags!
User is offlineProfile CardPM
+Quote Post

snoj
RE: Calculating An Average Of Mysql Row Data
15 Feb, 2007 - 01:52 PM
Post #8

Fell off the face of the earth
Group Icon

Joined: 31 Mar, 2003
Posts: 3,325



Thanked: 9 times
Dream Kudos: 750
My Contributions
You need to place $result in mysql_fetch_array(), mysql_fetch_assoc(), or mysql_result() to get any "data" out.

CODE

$row = mysql_fetch_array($result); //mysql_fetch_assoc() works almost the same as mysql_fetch_array.

//Or if you're just needing the average number.
$average = mysql_result($result, 0, 1); //0 is the row number, 1 is the field, in the case AVG(Overall).

User is offlineProfile CardPM
+Quote Post

icowpar
RE: Calculating An Average Of Mysql Row Data
21 Feb, 2007 - 11:15 AM
Post #9

New D.I.C Head
*

Joined: 15 Feb, 2007
Posts: 10


My Contributions
Still can't get it to output!
User is offlineProfile CardPM
+Quote Post

snoj
RE: Calculating An Average Of Mysql Row Data
21 Feb, 2007 - 11:41 AM
Post #10

Fell off the face of the earth
Group Icon

Joined: 31 Mar, 2003
Posts: 3,325



Thanked: 9 times
Dream Kudos: 750
My Contributions
Then you're doing something wrong and no one can help when we can't see what you're doing wrong.
User is offlineProfile CardPM
+Quote Post

icowpar
RE: Calculating An Average Of Mysql Row Data
21 Feb, 2007 - 11:47 AM
Post #11

New D.I.C Head
*

Joined: 15 Feb, 2007
Posts: 10


My Contributions
CODE

// Make the query.
$query = "SELECT AVG(Overall) FROM Master_Beer_Table WHERE Manufacturer = 'Boston Beer Company' GROUP BY Manufacturer";        
$result = @mysql_query ($query); // Run the query.
$row = mysql_fetch_array($result);
echo "<p>The average score is $row</p>";

User is offlineProfile CardPM
+Quote Post

snoj
RE: Calculating An Average Of Mysql Row Data
21 Feb, 2007 - 02:29 PM
Post #12

Fell off the face of the earth
Group Icon

Joined: 31 Mar, 2003
Posts: 3,325



Thanked: 9 times
Dream Kudos: 750
My Contributions
try $row['AVG(Overall)'] instead of just $row since $row is an array. wink2.gif
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:22AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month