dave
dave

Reputation: 1051

Getting PHP variables and their values from SQL COUNT() and GROUP BY

I'm trying to extract and make variables out of the "thinking skills" (like analyzing, evaluating, etc.) from a test and set their value to the number of test items within each. I'm stuck, so any help would be appreciated. (The SQL statement seems to work fine.)

Example of what I want: $analyzing = 7, $applying = 13, etc.... Thanks!

$sql = "SELECT  thinkskill        AS tskill
              , COUNT(thinkskill) AS counttskill
          FROM $c_keytable
      GROUP BY thinkskill
      ORDER BY thinkskill" ;
$result = mysql_query ( $sql ) ;

while ( $row = mysql_fetch_assoc ( $result ) )
{
   // Example: $analyzing = 7  --> 
   ${$row["tskill"]} = $row["counttskill"] ;
}

Upvotes: 0

Views: 683

Answers (1)

grossvogel
grossvogel

Reputation: 6782

Try this:

$summary = array ();
while ( $row = mysql_fetch_assoc ( $result ) )
{ 
   $summary[$row["tskill"]] = $row["counttskill"] ;
}
// now you can get the count for 'analyzing' with $summary['analyzing']

I don't recommend it, but if you really want the info out of the array and into local variables, you can do

extract ($summary)

Upvotes: 1

Related Questions