Bxx
Bxx

Reputation: 1625

MySQL Determine if Query Run when No Change to DB Fields

I want to determine if this script was run regardless if there was a change to the database.

For Example:

1) If name was previously "Joe" then $rowsAffect returns the 'correct' answer "1".

2) If name was previously "Bob" then $rowsAffect returns the 'INCORRECT' answer "0".

Is there a variable I can use that will tell me the script was run?

$query = "UPDATE table
          SET name='Bob'
          WHERE id=25";

$result =  $link->query($query);
$rowsAffect = mysqli_affected_rows($link);

Upvotes: 0

Views: 17

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269463

It you really care about updating rows, then you can add another column, something like LastUpdatedDate:

alter table t add column LastUpdatedDate datetime;

update table t
    set name = 'Bob', LastUpdatedDate = now()
    where id = 25;

This guarantees that the row is updated, so you will see a row count.

Of course, if you run the query and it doesn't fail, then it really did run.

Upvotes: 1

Related Questions