开发者

How to UPDATE a certain row OR RETURN FALSE if it doesn't exist?

开发者 https://www.devze.com 2023-01-20 23:27 出处:网络
How to 开发者_开发技巧do it in one query?You can do something like: function updateValue() { mysql_query($sql); // your update goes here

How to 开发者_开发技巧do it in one query?


You can do something like:

function updateValue()
{
     mysql_query($sql); // your update goes here
     return mysql_affected_rows() > 0;
}

From BoltClock's comment:

Bear in mind that mysql_affected_rows() returns 0 if a row exists but the old and new values are equal (meaning there was no need for an update).


You may use mysql_affected_rows() function to check, whether any rows were affected by update. See for details: http://php.net/manual/en/function.mysql-affected-rows.php


This is done for you already.

if(mysql_query('UPDATE table SET key = value WHERE id = 33')){/*....*/}

Using the where clause will check if the id exists and if it does then it will update it, what's the issue?

0

精彩评论

暂无评论...
验证码 换一张
取 消