使用PDO,如何确保UPDATE语句成功?

时间:2023-01-25 20:05:29

It seems that for an INSERT statement, one can use if (isset($connect->lastInsertId())) in order to check whether the INSERT statement was successful. (Please correct me if I'm wrong.)

对于INSERT语句,可以使用if ($connect->lastInsertId()))来检查INSERT语句是否成功。(如果我错了请纠正我。)

But for an UPDATE statement, how can I know if it was successful?

但是对于一个更新语句,我怎么知道它是否成功呢?

For example, I have basic one like that:

比如,我有一个基本的

$statement=$connect->prepare("UPDATE users SET premium='1' WHERE userid=?");
$statement->execute(array($id));

Thanks a lot in advance. Regards

非常感谢。问候

2 个解决方案

#1


20  

It depends on what you mean by "successful." If you mean that the query executed without failing, then PDO will either throw an exception on failure or return FALSE from PDOStatement::execute(), depending on what error mode you have set, so a "successful" query in that case would just be one in which the execute method did not return FALSE or throw an exception.

这取决于你所说的“成功”是什么意思。如果你意味着没有失败的查询执行,PDO将抛出一个异常失败或从PDOStatement返回FALSE::execute(),取决于你有什么错误模式设置,所以在这种情况下一个“成功”的查询是一个execute方法没有返回FALSE或抛出异常。

If you mean "successful" in that there were actually rows updated (versus just 0 rows updated), then you'd need to check that using PDOStatement::rowCount(), which will tell you the number of affected rows from the previous query.

如果您的意思是“成功”,因为实际上已经更新了行(而仅仅更新了0行),那么您需要使用PDOStatement:::rowCount()来检查,它将告诉您前面查询中受影响的行数。

Warning: For updates where newvalue = oldvalue PDOStatement::rowCount() returns zero. You can use

警告:对于newvalue = oldvalue PDOStatement::rowCount()返回零的更新。您可以使用

$p = new PDO($dsn, $u, $p, array(PDO::MYSQL_ATTR_FOUND_ROWS => true));

$p =新PDO($dsn, $u, $p,数组(PDO::MYSQL_ATTR_FOUND_ROWS => true);

in order to disable this unexpected behaviour.

为了阻止这种意想不到的行为。

#2


-1  

$stmt->execute();
$count = $stmt->rowCount();

if($count =='0'){
    echo "Failed !";
}
else{
    echo "Success !";
}

#1


20  

It depends on what you mean by "successful." If you mean that the query executed without failing, then PDO will either throw an exception on failure or return FALSE from PDOStatement::execute(), depending on what error mode you have set, so a "successful" query in that case would just be one in which the execute method did not return FALSE or throw an exception.

这取决于你所说的“成功”是什么意思。如果你意味着没有失败的查询执行,PDO将抛出一个异常失败或从PDOStatement返回FALSE::execute(),取决于你有什么错误模式设置,所以在这种情况下一个“成功”的查询是一个execute方法没有返回FALSE或抛出异常。

If you mean "successful" in that there were actually rows updated (versus just 0 rows updated), then you'd need to check that using PDOStatement::rowCount(), which will tell you the number of affected rows from the previous query.

如果您的意思是“成功”,因为实际上已经更新了行(而仅仅更新了0行),那么您需要使用PDOStatement:::rowCount()来检查,它将告诉您前面查询中受影响的行数。

Warning: For updates where newvalue = oldvalue PDOStatement::rowCount() returns zero. You can use

警告:对于newvalue = oldvalue PDOStatement::rowCount()返回零的更新。您可以使用

$p = new PDO($dsn, $u, $p, array(PDO::MYSQL_ATTR_FOUND_ROWS => true));

$p =新PDO($dsn, $u, $p,数组(PDO::MYSQL_ATTR_FOUND_ROWS => true);

in order to disable this unexpected behaviour.

为了阻止这种意想不到的行为。

#2


-1  

$stmt->execute();
$count = $stmt->rowCount();

if($count =='0'){
    echo "Failed !";
}
else{
    echo "Success !";
}