PDA

View Full Version : Issue calling and sorting data from phpmyadmin



tschweit
02-28-2012, 09:09 PM
I have a table that compiles survey data from people, and I want to be able to sort that data by gender. The survey asks them their gender, and I try to return the average of the "totalEA" field, but can't get it to work correctly. Thanks for any help you can provide.

Background info: The titles of "gender" and "totalEA" are both columns from the database. Here is a link of what I was trying to follow as well.http://www.tizag.com/mysqlTutorial/mysqlavg.php

This is the code that I can't get to work:



$queryfemale = "SELECT gender, AVG(totalEA) AS TotalFemale FROM $db_table GROUP BY gender";
$resultfemale = mysql_query($queryfemale);
printf("The totalEA for females is: ");
while ($rowfemale = mysql_fetch_assoc($resultfemale)){
printf(round($rowfemale['type'], 2));
printf(" ");


This code returns the totalEA value, not sorted by gender and works correctly, so I tried to model the above code off of it with no success.


$querytest = "SELECT totalEA AS Total FROM $db_table";
$resulttest = mysql_query($querytest);
printf("The results for the European-American section are: ");
while ($rowtest = mysql_fetch_assoc($resulttest)){
printf(round($rowtest['Total'], 2));
printf(" ");
}

tschweit
02-28-2012, 09:32 PM
New issue:

I got my code somewhat working, so here's the updated version.



$queryfemale = "SELECT totalEA AS TotalFemale FROM $db_table GROUP BY gender";
$resultfemale = mysql_query($queryfemale);
while ($rowfemale = mysql_fetch_assoc($resultfemale)){
printf("The totalEA for females is: ");
printf(round($rowfemale['TotalFemale'], 2));
printf("and ");
}


Now, it is sorting by gender like I want, and returning both the female and male values from this table. However, I can't figure out how to word the "While" part of the code. Currently it repeats itself, giving this result:

The totalEA for females is: 0 The totalEA for females is: 28

I want the second sentence to read "male" instead of "female", but I don't know how to change that within the while statement while still outputting both the male and female totals. Any help?