从 SQL COUNT() 和 GROUP BY 获取 PHP 变量及其值

发布于 2024-09-08 09:54:17 字数 513 浏览 4 评论 0原文

我试图从测试中的“思维技能”(如分析、评估等)中提取并生成变量,并将其值设置为每个测试项目的数量。我被困住了,所以任何帮助将不胜感激。 (SQL 语句似乎工作正常。)

我想要的示例:$analyzing = 7、$applying = 13 等...谢谢!

$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"] ;
}

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"] ;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明天过后 2024-09-15 09:54:17

试试这个:

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

我不推荐它,但如果你真的想要将信息从数组中取出并放入局部变量中,你可以这样做

extract ($summary)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文