Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the row has been updated?

Tags:

How to return a boolean if the record has been updated on the database?

Example:

try {   $SQL = "UPDATE addressbook SET valid = '0' WHERE id = :id";   $query = $this->db->prepare($SQL);   $query->bindValue(":id", $id);   $query->execute();    //How do I know if record has been updated?  } catch (PDOException $e) {   $j['success'] = 'false';   echo json_encode($j);   return; } 
like image 685
I'll-Be-Back Avatar asked Oct 11 '11 19:10

I'll-Be-Back


People also ask

How do you check whether the row data has been changed?

One way is to start a transaction, select the contents of the row and compare it to what you're going to update it to. If they don't match, then do the update and end the transaction. If they match, rollback the transaction.

How can I tell when a SQL row was added?

If you want to know when a row is inserted, the easiest thing would be to simply add a date or timestamp field with a default value (like getDate()) that automatically fills in the date/time when the row is inserted.

How do I find the last updated row in SQL?

To get the last updated record in SQL Server: We can write trigger (which automatically fires) i.e. whenever there is a change (update) that occurs on a row, the “lastupdatedby” column value should get updated by the current timestamp.

How do I check for an update in SQL?

SQL UPDATE Syntax To use the UPDATE method, you first determine which table you need to update with UPDATE table_name . After that, you write what kind of change you want to make to the record with the SET statement. Finally, you use a WHERE clause to select which records to change.


2 Answers

You can use:

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

PDOStatement::rowCount (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)

To your particular case, returning a boolean:

return $query->rowCount() ? true : false; 
like image 68
CD001 Avatar answered Oct 07 '22 18:10

CD001


If something went wrong but your code was updated in the database, that sounds like a really precarious state to be in. It's probably better to use a transaction and rollback on exception.

Something like (untested):

$this->db->beginTransaction(); try {        $SQL = "UPDATE addressbook SET valid = '0' WHERE id = :id";       $query = $this->db->prepare($SQL);       $query->bindValue(":id", $id);       $query->execute();        $this->db->commit();        return true;        //How do I know if record has been updated?  } catch (PDOException $e) {       $this->db->rollback();       return false; } 

Also, you probably don't want to mix your JSON in with this code, separate it out and have something outside your class deal with JSON.

like image 39
Levi Morrison Avatar answered Oct 07 '22 20:10

Levi Morrison