How to do it in one query?
+3
A:
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).
Pablo Santa Cruz
2010-10-24 13:06:00
Bear in mind that `mysql_affected_rows()` does not count a row if its old and new values are equal (meaning there was no need to update it). For most cases, though, this does the trick.
BoltClock
2010-10-24 13:07:33
@BoltClock: good point. Will update the answer.
Pablo Santa Cruz
2010-10-24 13:08:33
Must count the equal values as a successful update... So that does not work, unfortunately.
Cover
2010-10-24 13:18:51
+1
A:
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
Kel
2010-10-24 13:06:04
+1
A:
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?
RobertPitt
2010-10-24 13:07:32