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.... 开发者_Go百科 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"] ;
}
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)
精彩评论