views:

1029

answers:

5

How do i find if my update is successful or not? i update using a where uniqueName=name so i should always only update 0 rows or 1. What is a good way to check if i updated a row or not?

+1  A: 

Use mysql_affected_rows.

Josh Leitzel
This might return 0 if the data updated in the query is the same as already in the database, so it might return 0 even if the query was successful but 0 rows were changed. But i assume your use case wont have this scenario.
andho
+2  A: 

mysql_affected_rows() will return the number of rows affected by your update.

http://us.php.net/manual/en/function.mysql-affected-rows.php

brianreavis
A: 

if your update query is in a variable named $query, you can do;

$result = mysql_query($query);
if($result){
    //succes!
} else {
    //fail!
}

Hope this helps. If you just want to know if your query was executed succesfully, this will do. If you really want to know how many rows are affected, use the other suggestions.

Lex
This will only return `false` if the query was malformed. Even if the `UPDATE` operates on zero rows, this will show "success!"
brianreavis
This will only tell you if the query was executed successfully, not how many rows were affected.
Josh Leitzel
+1  A: 

try mysql_affected_rows()

rubayeet
+1  A: 

There is really no way of knowing it. Let's say the table tbl_numbers(id, value) has rows (1,"one") and (2,"two");

$result1="update tbl_numbers set value='first' where id=1"; If you check $result1 in if clause it returns true and mysql_affected_rows() returns 1.

However, for $result2="update tbl_numbers set value='two' where id=2"; If you check $result2 in if clause it returns true and mysql_affected_rows() returns 0.

And, for $result3="update tbl_numbers set value='three' where id=3"; If you check $result3 in if clause it returns true and mysql_affected_rows() returns 0.

Karthikeya