tags:

views:

50

answers:

3

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
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
@BoltClock: good point. Will update the answer.
Pablo Santa Cruz
Must count the equal values as a successful update... So that does not work, unfortunately.
Cover
+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
+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
The program wants to know whether it exists and is successfully updated or it does not exist.
Cover
Can you please make that requirement more clear in your inital question then?
DanMan