tags:

views:

51

answers:

1

Hi guys.

I have a table with 8 fields and a corresponding form. When the form is submitted, I have the id of the record. What is the best way to check if data in any one of the fields has changed for that row? Should use the SQL route

WHERE field1=$field1 AND field2=$field2 ..... AND ID=$ID

or pull the row and compare each field with a loop? Any other ways of achieving this?

Thanks!

A: 

Depends on what you need to do...

For example, some people check login details like so (assume all variables are escaped correctly).

SELECT `id`
  FROM `users`
 WHERE `username` = $username
   AND `password` = $password

But what happens if you want to log if the username was wrong or the password was wrong? (of course you should never tell the user this).

In that case, I'd do...

SELECT `id`, `password`
  FROM `users`
 WHERE `username` = $username

If that fails, the username does not exist. You can then internally log this.

Then you can do (sha1($salt . $password)) === $passwordFromDb) to be able to log if the username exists, but password was incorrect.

However, if you don't need this level of logic, just compare in the SQL query. It will be more succinct and quicker to boot.

alex
Thanks, I will stick with the SQL method.
pistolshrimp