i have used NOW() function in query but it always show 0000-00-00 00:00:00
please tell how to use this to show current date and time.
how to use NOW()
Moderator: General Moderators
Re: how to use NOW()
it is only for mysql aactually so if u want to insert the current date than use it into the query like this
insert into tablename (currentdate) values (now());
insert into tablename (currentdate) values (now());
Re: how to use NOW()
Show us your query, and tell us what version of which database engine you are using.pradip wrote:i have used NOW() function in query but it always show 0000-00-00 00:00:00
please tell how to use this to show current date and time.
Re: how to use NOW()
I have used in query of the form:
"UPDATE userinfo SET lastlogin = NOW() WHERE uname='$username'";
with MySQL 5.0
"UPDATE userinfo SET lastlogin = NOW() WHERE uname='$username'";
with MySQL 5.0
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: how to use NOW()
I personally have never used now() in fear that the results might somehow differ from that of the code most of my logic relies on...
I just initialize a variable from mktime() or time() and use string interpolation or the C style sprintf, etc...
Not saying now() is wrong...just at least when you dump the SQL to screen when debugging the timestamp value is obviously.
I just initialize a variable from mktime() or time() and use string interpolation or the C style sprintf, etc...
Not saying now() is wrong...just at least when you dump the SQL to screen when debugging the timestamp value is obviously.
- blacksnday
- Forum Contributor
- Posts: 252
- Joined: Sat Jul 30, 2005 6:11 am
- Location: bfe Ohio :(
Re: how to use NOW()
I know this doesnt answer your question But I had to add this:pradip wrote:I have used in query of the form:
"UPDATE userinfo SET lastlogin = NOW() WHERE uname='$username' ";
with MySQL 5.0
One thing you should add to that query at the end is: LIMIT 1
such as:
"UPDATE userinfo SET lastlogin = NOW() WHERE uname='$username' LIMIT 1";
plus based on that query and me using mySQL4.1.22-standard I use same query basically and it works fine on my server, did mySQL change for future versions I should be aware of?
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: how to use NOW()
Why? As long as the username is unique only that record gets updated. LIMIT 1 could lead to some weird bugs and is somewhat hackish.One thing you should add to that query at the end is: LIMIT 1
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: how to use NOW()
I use NOW() in all of my queries that need the now time in the format "YYYY-MM-DD HH:MM:SS" without issue.
I would run a simple select to see if your mysql server is understanding that function, and if not, to see if the server can even serve time/date data:
I would run a simple select to see if your mysql server is understanding that function, and if not, to see if the server can even serve time/date data:
Code: Select all
SELECT NOW() AS `right_now`;