How to determine if a MySQL update query succeeded when the data passed in the query is the same as what is already in the database?

蹲街弑〆低调 提交于 2019-12-04 03:07:28

Just check if no errors occurred after execution of query.
If you use mysql, check mysql_error():
if (!mysql_error()) print 'all is fine';
Same for mysqli.

Variation 1:

mysql_query() or die('error');

Variation 2:

$conn = mysql_query();
if(!$conn) {//Error code here}

Variation 3:

try {
  $conn = mysql_query();
  if (!$conn) throw new Exception("mysql Error");
} catch(Exception $e) {
  echo $e->getMessage();
}
Mel

[affected_rows()][1] is -1 if a query fails, not zero.

[1]: http://www.php.net/manual/en/function.mysql-affected-rows.php

It may return 0 if no changes were made to the row (same values), or if mysql didnt find a row to update. It will only return -1 due syntax erro

James

if the update "fails" due to syntax error, or other mysql will return an error code on the actual mysql query and affected_rows will return with yet another error.

Php for example:

$qry = mysql_query("update blah where IamaSyntaxerror,33");
if ($qry === FALSE) { echo "an error has occured"; }

else  { mysql_affected_rows() == 0  means no updates occured
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!