0

我正在尝试确定第一次尝试或第二次尝试通过测试的学生人数的百分比。我相信我的代码是正确的,但是当我回显结果时,我没有得到任何输出。

<?php
$NREMT1 = "SELECT nremtcognitive FROM course_students WHERE nremtcognitive = '1' ";
$NREMT2 = "SELECT nremtcognitive FROM course_students WHERE nremtcognitive >= '2' ";
$completed = "SELECT studentstatus FROM course_students WHERE studentstatus >='4'";

$getNREMT1 = mysql_query($NREMT1);
$fetchNREMT1= mysql_num_rows($getNREMT1);
$getNREMT2 = mysql_query($NREMT2);
$fetchNREMT2= mysql_num_rows($getNREMT2);
$getcompleted = mysql_query($completed);
$fetchcompleted= mysql_num_rows($getcompleted);

function percent($fetchNREMT1, $fetchNREMT2, $fetchcompleted) {
    $NREMT1count= $fetchNREMT1/$fetchcompleted;
    $NREMT1percent= $NREMT1count * 100;
    $NREMT1result = number_format($NREMT1percent,0);

    $NREMT2count= $fetchNREMT2/$fetchcompleted;
    $NREMT2percent= $NREMT2count * 100;
    $NREMT2result = number_format($NREMT2percent,0);
}
echo $NREMT1result;
echo $NREMT2result;

?>
4

3 回答 3

0

你没有在percent任何地方调用你的函数。

你应该调用它并让它返回一些东西,否则就可以避免创建它并将相同的代码放在同一个地方:

 $NREMT1count= $fetchNREMT1/$fetchcompleted;
 $NREMT1percent= $NREMT1count * 100;
 $NREMT1result = number_format($NREMT1percent,0);

 $NREMT2count= $fetchNREMT2/$fetchcompleted;
 $NREMT2percent= $NREMT2count * 100;
 $NREMT2result = number_format($NREMT2percent,0);

 echo $NREMT1result;
 echo $NREMT2result;

看来你对function的概念不是很清楚。你可以看看文档

于 2013-03-26T16:14:59.833 回答
0

只需更改您的查询。未经测试,但这应该让你开始:

SELECT 
    SUM(IF(CONVERT(nremtcognitive, UNSIGNED INTEGER) >= 2)) / COUNT(*) 
FROM course_students;
于 2013-03-26T16:18:04.027 回答
0

您的问题是内部声明的变量percent()从未被初始化,因为该函数从未被调用。尝试这个:

<?php
$NREMT1 = "SELECT nremtcognitive FROM course_students WHERE nremtcognitive = '1' ";
$NREMT2 = "SELECT nremtcognitive FROM course_students WHERE nremtcognitive >= '2' ";
$completed = "SELECT studentstatus FROM course_students WHERE studentstatus >='4'";

$getNREMT1 = mysql_query($NREMT1);
$fetchNREMT1= mysql_num_rows($getNREMT1);
$getNREMT2 = mysql_query($NREMT2);
$fetchNREMT2= mysql_num_rows($getNREMT2);
$getcompleted = mysql_query($completed);
$fetchcompleted= mysql_num_rows($getcompleted);

percent($fetchNREMT1, $fetchNREMT2, $fetchcompleted);

function percent($fetchNREMT1, $fetchNREMT2, $fetchcompleted) {
    $NREMT1count= $fetchNREMT1/$fetchcompleted;
    $NREMT1percent= $NREMT1count * 100;
    $NREMT1result = number_format($NREMT1percent,0);

    $NREMT2count= $fetchNREMT2/$fetchcompleted;
    $NREMT2percent= $NREMT2count * 100;
    $NREMT2result = number_format($NREMT2percent,0);

    echo $NREMT1result;
    echo $NREMT2result;
}
?>

还值得注意的是,mysql_*现在不推荐使用该函数系列,您应该真正考虑使用 MySQLi 或 PDO。

于 2013-03-26T16:16:16.460 回答