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
Use mysql_affected_rows.
If you are using prepared statement..
//query
$stmt = $conn->prepare("UPDATE user SET name = 'Jack' WHERE id = ?");
//bind parameters
$stmt->bind_param("i", $id);
//execute
$stmt->execute();
//updated row count
echo $stmt->affected_rows;
echo " rows updated";
So what if the user resubmits data that is already the same as the information in the DB? If memory serves that would return mysql_affected_rows() with a value of 0 because by definition no rows were updated in the process. Yet the query itself was successful.
The workaround to this would be to either proof your content before insertion or using:
$result = mysql_query($q);
if( mysql_affected_rows() == 0 )
$state = $result ? "Success" : "Fail";
This way you get to check if the rows were updated and if not you can check if it was a malfunction or just repeat data.
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.