liewl
liewl

Reputation: 4071

PHP/MySQL: Updating nonexistent column value

So, this query:

mysql_query("UPDATE item SET name = 'foo' WHERE name = 'bar'");

is returning 1, but the value 'bar' doesn't exists in the table. As expected, nothing has changed in the database itself, but shouldn't mysql_query() return 0 in that case?

Upvotes: 3

Views: 585

Answers (3)

deceze
deceze

Reputation: 522342

Why, no. The query itself was successful, i.e. it was a valid query and was successfully executed. It just didn't apply to any row.

Upvotes: 1

methodin
methodin

Reputation: 6710

If you are just echoing the value of mysql_query, it would be true or false. You need to use mysql_affected_rows() to get the actual affected rows.

Upvotes: 1

Mischa
Mischa

Reputation: 43298

It returns true, because the query was executed successfully. If you want to know how many rows were updated you have to use mysql_affected_rows.

Upvotes: 5

Related Questions