在PDO中,我可以执行()一个字符串,但不能准备()然后执行()


In PDO, I can exec() a string, but not prepare() and then execute() it

如果我PDO::exec()是一个查询,它就可以工作。但是,当我尝试先使用PDO::prepare(),然后使用PDO::execute()时,不起作用。为什么?

例如,这正如预期的那样:

$db->exec($string);

但事实并非如此:

$stmt = $db->prepare($string);
$stmt->execute();

没有抛出任何错误,并且$db->errorInfo显示为全零,这意味着成功。

这对我来说毫无意义。

编辑上下文

注意,newDB()只是一个用我这里需要的设置启动DB的函数。

当我运行以下程序时,它按预期工作:

$db = newDB();
if (!$error) {
    if (isset($id)) {
        try {
            $db->exec('UPDATE Events SET Title = "WHY" , Date = "2012-11-01 00:00:00", Place = "no", Description = "no", UDate = now() WHERE Id = "12"');
        } catch(PDOException $e) {
            $error = 1;
        }
    }
}
$db = null;

然而,这并没有,我也不知道为什么:

$db = newDB();
if (!$error) {
    if (isset($id)) {    
        try {
            $stmt = $db->prepare('UPDATE Events SET Title = "WHY" , Date = "2012-11-01 00:00:00", Place = "no", Description = "no", UDate = now() WHERE Id = "12"');
            $stmt->execute();
        } catch(PDOException $e){
            $error = 1;
        }
    }
}
$db = null;

您设置了异常模式吗?

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
相关文章: