Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible Delay When Using NOW() in MySQL Query?

Tags:

mysql

I'm learning MySQL and PHP through a book at the library. I was improving the security of the password encrypting system by changing the password storage from

password=SHA('password')

to

password=SHA(CONCAT('password', '--', registration_date))

where registration_date is the timestamp when the user registered.

The current code for registering users is:

INSERT INTO users (first_name, last_name, email, password, registration_date) 
VALUES ('first_name', 'last_name', 'email', SHA(CONCAT('password', '--', NOW())), NOW());

Will I need to worry about the two different NOW() functions in there? Is there a possibility of them having slightly different times? I tried it with a couple of queries and it seemed to work ok.

If there is a problem, how would I fix it?

like image 354
Max Avatar asked Oct 25 '22 00:10

Max


1 Answers

There's no problem. From the manual:

NOW() returns a constant time that indicates the time at which the statement began to execute. (Within a stored function or trigger, NOW() returns the time at which the function or triggering statement began to execute.)

As the manual shows by example, every evaluation of NOW() within a statement returns the same value, regardless of how much time has passed between the evaluations.

like image 92
Ted Hopp Avatar answered Oct 27 '22 11:10

Ted Hopp