php - PDO:bindParam令牌语法问题

时间:2012-08-05 14:06:15

标签: php mysql pdo

是否有针对此错误的解决方案?是否因为查询中的mySQL时间格式而发生?

SQL Error
Error:SQLSTATE[HY093]: Invalid parameter number: number of bound variables 
does not match number of tokens 

Array
(
    [:service_user_id] => 90
    [:week_beginning] => 2012-08-06
    [:week_ending] => 2012-08-12
)
Backtrace:C:\wamp\www\Sitetest_9.6.12\public_html\main\ajax\timetable_grid_load.php at line 45 

这是使用php-pdo-wrapper-class的PHP:

$bind = array(
    ":service_user_id" =>  $service_user_id,
    ":week_beginning" => $week_beginning,
    ":week_ending" => $week_ending,
);


$query = "SELECT 
    id AS sessID,
    session_day as sessDay,
    session_type_id,
    provider_id,
    description,
    TIME_FORMAT(start_time, '%H:%i') as start_time,
    TIME_FORMAT(finish_time, '%H:%i') as finish_time,
    start_date,
    finish_date,
    (SELECT absence FROM attendance WHERE sessID = session_id AND absence_date = DATE_ADD(':week_beginning', INTERVAL sessDay-1 DAY)) AS attendance
    FROM
    sessions
    WHERE
    service_user_id = :service_user_id AND
    start_date <= ':week_ending' AND
    (finish_date >= ':week_beginning' OR
    finish_date IS NULL OR 
    finish_date=0)
    ORDER BY session_day ASC";      

$result= $db->run($query,$bind);
return $result;

1 个答案:

答案 0 :(得分:3)

不要使用单引号来分隔预准备语句中的参数。这不是必要的(这是首先准备好陈述的重点)。

    ...
WHERE
    service_user_id = :service_user_id AND
    start_date <= :week_ending AND
    (finish_date >= :week_beginning OR
    finish_date IS NULL OR 
    ...

提示:start_date <= ':week_ending'转换为less than or equal to the literal string ":week_ending"