{“status”:{“error”:“SQLSTATE [HY093]:参数号无效:绑定变量数与令牌数不匹配”}}

时间:2016-10-12 06:34:51

标签: php mysql slim

$app->get('/view/appointment/:databaseID/:userID/:appointmentID', function($databaseID, $userID, $appointmentID) use($app) {

    $params['databaseID'] = $databaseID; //$request->getAttribute('databaseID');
    $id = $userID; //$request->getAttribute('userID');    
    $date = $appointmentID; //$request->getAttribute('appointmentID');

    $sql = "SELECT imapt_date, imapt_start_time, imapt_end_time, imapt_patient_id, imapt_procedure
        from im_ap_timetable inner join im_practioner on
        im_ap_timetable.impract_id=im_practioner.impract_id inner join  
        im_users_cd on im_practioner.user_id=im_users_cd.user_id
        where im_users_cd.user_id = :id and im_ap_timetable.imapt_date = :date";

    try {
        $db = getDB($params['databaseID']);
        $stmt = $db->prepare($sql);
        $query = compact('id');
        $stmt->execute($query);

        $patient = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $status = array('success' => '200');

        echo json_encode(compact('patient','status'));

    } catch (Exception $e) {

        $status = array('error' => $e->getMessage());

        echo json_encode(compact('status'));

    }

});

2 个答案:

答案 0 :(得分:1)

您有两个占位符::id:date

但你只绑定了一个 - :id

您也需要绑定:date。在你的情况下它是:

$query = compact('id', 'date');

答案 1 :(得分:0)

您可以使用bindParam方法将参数绑定到SQL查询。

http://php.net/manual/en/pdostatement.bindparam.php

您也可以将它们传递到execute方法中的数组中。

$stmt->execute($query, [':id' => $id, ':date' => $date]);