用特殊字符更新教义

update in Doctrine with special characters

当我尝试用特殊字符更新我的值时出现此错误

Message: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't' WHERE (idcommentaire = 117)' at line 1. Failing Query: "UPDATE commentaire SET commentaire = 'test't' WHERE (idcommentaire = 117)"

UPDATE commentaire SET commentaire = 'test't' WHERE (idcommentaire = 117)
                                          ^

为什么学说不管理特殊字符?

我的函数:

static public function modifierCommentaire($id, $commentaire)
{
    $req = Doctrine_Query::create()
    ->update('Commentaire c')
    ->set('c.commentaire ', $commentaire)
    ->where("c.idcommentaire=$id")
    ->execute();
}

您应该使用准备好的语句,更新查询应该如下所示:

Doctrine_Query::create()
  ->update('Commentaire c')
  ->set('c.commentaire', '?', $commentaire)
  ->where('c.idcommentaire = ?', $id)
  ->execute();

所以在你有变量的地方放一个 ? 并将参数作为参数传递。这样 doctrine 将创建准备好的语句并且变量将被正确转义(并且它也更有效)。