如何让带有绑定变量的 where 子句在 php 中工作


How to get the where clause with a bind variable to work in php

function get_grade($sku) {
    require("config.php");
    try {
        $results = $db -> query prepare ("SELECT name, subject1,grade1,attendance,gender,subject2,grade2,subject3,grade3 FROM student WHERE sku = ?"); //binds the sku to the question mark
        $results -> bindParam;
        $results -> execute();
    }
    catch exception ($e) {
        echo "could not connect";
        exit;
    }
    $product = $results-> fetch (PDO::FETCH_ASOC);
}

如何使 where 子句起作用,如果用户具有相同或相似的出勤率、科目和成绩,那么他们将获得该成绩,然后他们将从过去的学生那里获得该成绩。

您需要绑定$sku

$results->bindParam(1, $sku);

这应该适合您:

function get_grade($sku) {
    require("config.php");
    try {
        $results = $db->prepare("SELECT name, subject1,grade1,attendance,gender,subject2,grade2,subject3,grade3 FROM student WHERE sku = :sku"); 
                     //^^^^^^^^Perpare statement here                                                                                  //^^^^Placeholder for variable
        $results->execute(array("sku" => $sku));
                        //^^^^^^^^^^^^^^^^^^^Instead of binParam
    } catch (PDOException $e) {
           //^^^^^^^^^^^^^^^ Catch PDO exeption
        echo $e->getMessage();
        exit;
    }
    $product = $results->fetch(PDO::FETCH_ASOC);

}