Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell when a MySQL UPDATE was successful versus actually updated data?

How do I tell when a MySQL UPDATE was successful versus actually updated data?

Example:

TABLE
id    city_name
1     Union
2     Marthasville

If I run the following:

$data = array('city_name', 'Marthasville');

//update record 2 from Marthasville to the same thing, Marthasville. 
$this->db->where('id', 2);
$this->db->update('table', $data);

if($this->db->affected_rows() > 0)
{
    //I need it to return TRUE when the MySQL was successful even if nothing was actually updated.
    return TRUE;
}else{
    return FALSE;
}

This will return TRUE every time the UPDATE statement is successful, but FALSE when no rows were actually updated.

I need it to return TRUE every time the UPDATE statement was successfully executed even if it doesn't actually change any records.

like image 648
zechdc Avatar asked Sep 09 '11 22:09

zechdc


People also ask

How do I know if SQL update was successful?

You can use @@ROWCOUNT to get the number of rows affected by the last query. This can be used to decide whether your WHERE clause actually matched something, for example. Show activity on this post. You can use the return value of the ExecuteNonQuery to check if the update was successful or not.

How can I tell when a MySQL table was last updated?

Syntax to know the last updated time. SELECT UPDATE_TIME FROM information_schema. tables WHERE TABLE_SCHEMA = 'yourDatabaseName' AND TABLE_NAME = 'yourTableName'; Let us implement the following query to get the last updated time.

How long does MySQL update take?

All date and time column types are converted to the new format, which can take several hours to days. The duration depends on the amount of data in your tables. For more information, see Upgrades to MySQL version 5.7 might be slow.

Does MySQL update automatically?

Any server with a local installation of MySQL 5.5 and no databases will automatically update to MySQL 5.7 or newer.


2 Answers

Have a look at mysql_affected_rows()

It should tell you if anything was actually updated as opposed to nothing was successfully updated resulting in a return of true.

php.net says:

mysql_affected_rows()

Returns the number of affected rows on success, and -1 if the last query failed.

You could use the following to achieve your desired results:

if($this->db->affected_rows() >= 0){ }
like image 191
472084 Avatar answered Oct 05 '22 18:10

472084


Then you would use mysql_query:

SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

Simple like this:

$result = $this->db->update('table', $data);

if($result)
{
     //without error even no row updated
} else {

}
like image 45
lnguyen55 Avatar answered Oct 05 '22 19:10

lnguyen55