如何使用绑定变量获取where子句以在php中工作

时间:2015-01-05 22:35:34

标签: php sql pdo

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子句起作用,如果用户具有相同或相似的出勤率,科目和成绩,那么他们将获得然后他们将从过去的学生获得该等级。

2 个答案:

答案 0 :(得分:4)

您需要绑定$sku

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

答案 1 :(得分:1)

这应该适合你:

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);


}