Last active
August 29, 2015 14:04
-
-
Save zerowebcorp/1685b552ee79afb23733 to your computer and use it in GitHub Desktop.
PHP Snippt to get the changes caused by the UPDATE query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Get the entire row before we update and save it in $row | |
$row = $db -> where('id', $id) -> from('tblorders') -> fetch_first(); | |
// This will execute SELECT * FROM tblorders WHERE id = '12' ; I am using PHP | |
// MYSQLi wrapper from https://bitbucket.org/getvivekv/php-mysqli-class/ | |
// Now perform the UPDATE query | |
$data['email'] = 'someemail'; | |
$data['firstname'] = 'Vivek'; | |
$data['lastname'] = 'V'; | |
$db -> where('id', $id) -> update('tblorders'); | |
// This will execute UPDATE tblorders SET email='someemail', firstname='Vivek', | |
// lastname='V' WHERE id='12'; | |
// Now get the new row | |
$row2 = $db -> where('id', $id) -> from('tblorders') -> fetch_first(); | |
// Now do the comparison | |
foreach ($row as $key => $val) | |
{ | |
if ($row2[$key] != $val) | |
$changed[] = $key; | |
} | |
// Now the array $changed contains all the changed/updated values. You can | |
// display those values as per your requirement | |
if (is_array($changed)) | |
{ | |
foreach ($changed as $key) | |
{ | |
$changes .= "$key - $row[$key] => $row2[$key] "; | |
} | |
} | |
echo $changes; | |
// Will print as a string | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment