I have a table with a text column called myTextColumn. I would like to do something in the PHP application only if an UPDATE changed the value. I am not using triggers. Is there a better way to do so other than the following? Thank you
function updateTable($data)
{
$sql='SELECT id FROM myTable WHERE id=:id AND myTextColumn!=:myTextColumn';
$stmt=db::db()->prepare($sql);
$stmt->execute($data);
if($stmt->fetchColumn()) {
$sql='UPDATE myTable SET myTextColumn=? WHERE id=?';
$stmt=db::db()->prepare($sql);
$stmt->execute($data);
return true;
}
else {return false;}
}
if(updateTable(array('id'=>123, 'myTextColumn'=>'Some text goes here')))
{
echo('It Changed');
}
You can check the rowCount after your PDO call. It'll tell you the number of rows affected by your UPDATE.
$sql='UPDATE myTable SET myTextColumn=? WHERE id=?';
$stmt=db::db()->prepare($sql);
$stmt->execute($data);
if ( $stmt->rowCount() > 0 )
{ /* do stuff */ }