当前位置:优学网  >  在线题库

如何在数据库中插入2个变量的商,然后将其作为百分比回显

发表时间:2022-07-31 01:43:26 阅读:205
$score = 50
$total = 500

mysqli_query($conn,"UPDATE student_quiz SET `grade` = ".$score." out of ".$total."')

因此,目前这将在数据库中存储"500分之50",但我想它做的是存储$score和$total的商,因此它应该是"0.1",而不是"500分50".

当我显示它时,我如何将其作为百分比,这样当我回显$grade时,它将变为"10%",而不是"0.1".目前,这是我获取和显示分数的方式.

$query1 = mysqli_query($conn,"select * from student_class_quiz where class_quiz_id = '$id' and student_id = '$session_id'")or die(mysqli_error());

    $row1 = mysqli_fetch_array($query1);
    $grade = $row1['grade'];
   <?php echo $grade; ?>
🎖️ 优质答案
  • 用`$score/$total计算商.

    $score = 50;
    $total = 500;
    $quotient = $score/$total;
    $stmt = mysqli_prepare($conn,"UPDATE student_quiz SET `grade` = ?");
    $stmt->bind_param("s", $quotient);
    $stmt->execute();
    

    要将其显示为百分比,请乘以100.

    <?php
    $query1 = mysqli_prepare($conn,"select * from student_class_quiz where class_quiz_id = ? and student_id = ?") or die(mysqli_error($conn));
    $query1->bind_param("ss", $id, $session_id);
    $query1->execute();
    $result = $query1->get_result();
    $row1 = $result->fetch_assoc($query1);
    $grade = $row1['grade'];
    echo "Grade percentage is " . round($grade*100) . "%";
    
  • 相关问题