2

有解决此错误的方法吗?是因为查询中的 mySQL 时间格式而发生的吗?

SQL 错误
错误:SQLSTATE[HY093]:无效的参数号:绑定变量的数量
与令牌数量不匹配

大批
(
    [:service_user_id] => 90
    [:week_beginning] => 2012-08-06
    [:week_ending] => 2012-08-12
)
回溯:C:\wamp\www\Sitetest_9.6.12\public_html\main\ajax\timetable_grid_load.php 在第 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;
4

1 回答 1

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".

于 2012-08-05T14:14:13.233 回答