dtpngq3378499 2015-08-11 14:08
浏览 134
已采纳

使用PDO命名参数抛出错误:SQLSTATE [HY000]:常规错误:1接近“(”:语法错误

I am attempting to update my SQLite database using:

$insertion = $db->prepare("UPDATE roles SET ($set_string) WHERE entryDate = :entryDate AND name = :name");
$insertion->execute($assoc_data);   

Here $set_string is a string of attributes and parameters, which are what I want. A print_r() of $set_string yields:

name = :name,
entryDate = :entryDate,
development = :development,
adhoc = :adhoc,
mentoring = :mentoring,
documentation = :documentation,
codeReview = :codeReview,
meetings = :meetings,
learning = :learning,
waste = :waste,
workPrioritization = :workPrioritization,
management = :management,
applicationTraining = :applicationTraining

$assoc_data is an associative array with all the data that I want to update the database with:

Array (
    [name] => Bobby Bob
    [entryDate] => 2015-08-11
    [development] => 0
    [adhoc] => 0
    [mentoring] => 0
    [documentation] => 0
    [codeReview] => 0
    [meetings] => 0
    [learning] => 0
    [waste] => 0
    [workPrioritization] => 0
    [management] => 0
    [applicationTraining] => 100
)

I get the following error:

Exception: SQLSTATE[HY000]: General error: 1 near "(": syntax error

Could someone pinpoint what the error is? I guess I don't quite understand how to use named parameters.

  • 写回答

1条回答 默认 最新

  • dongtan7351 2015-08-11 14:53
    关注

    So your first problem is, that your SQL UPDATE syntax is wrong. You don't need to use parentheses, as you can see from the documentation:

    UPDATE [LOW_PRIORITY] [IGNORE] table_reference
        SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
        [WHERE where_condition]
        [ORDER BY ...]
        [LIMIT row_count]
    

    Second placeholders have to be unique. This means you can only use them once for one value. As you can read in the PHP manual:

    You must include a unique parameter marker for each value you wish to pass in to the statement when you call PDOStatement::execute().

    And last name is a mysql reserved word, so you should put ` around it.

    Means first remove your parentheses and second use unique placeholders (You can't have 2x :name and :entryDate) and put your backticks around reserved words.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?