Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to UPDATE a certain row OR RETURN FALSE if it doesn't exist?

Tags:

php

mysql

How to do it in one query?

like image 797
Cover Avatar asked Dec 30 '25 11:12

Cover


2 Answers

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).

like image 75
Pablo Santa Cruz Avatar answered Jan 01 '26 04:01

Pablo Santa Cruz


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

like image 22
Kel Avatar answered Jan 01 '26 02:01

Kel