代码之家  ›  专栏  ›  技术社区  ›  SteD

PDO准备插入序列化数据错误

  •  1
  • SteD  · 技术社区  · 14 年前

    我试图使用PDO将序列化数据插入到mySQL中,但遇到了一些语法错误。我错过了什么吗?

    一些简化编码:

    $test['1'] = "one";
    $condition = serialize($test);
    $stmt = $dbh->prepare("INSERT INTO weather(condition) VALUES (:condition)");
    $stmt->bindParam(":condition",$condition);
    $stmt->execute();
    

    $stmt->debugDumpParams() 显示

    SQL: [67] INSERT INTO weather(condition)
    

    名称:[10]:条件参数-1

    print_r($stmt->errorInfo()) 显示

    数组([0]=>42000[1]=>1064[2] =>您的SQL语法有错误;请检查手册 正确语法的版本 接近条件)值 ('a:1:{i:1;s:3:\“one\”;}')在第1行

    2 回复  |  直到 14 年前
        1
  •  2
  •   Ondrej Slinták    14 年前

    其实解决办法很简单。 Condition 是MySQL中的保留字。不能将它用作列的名称。

    你可以在他们的网页上找到MySQL保留字的完整列表 here here .

        2
  •  0
  •   Alec    14 年前

    $stmt->bindParam(':condition',$condition,PDO::PARAM_STR);