Page 1 of 1
how to use NOW()
Posted: Wed Feb 20, 2008 11:49 pm
by pradip
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()
Posted: Wed Feb 20, 2008 11:56 pm
by djdon11
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());
Re: how to use NOW()
Posted: Thu Feb 21, 2008 10:08 pm
by califdon
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.
Show us your query, and tell us what version of which database engine you are using.
Re: how to use NOW()
Posted: Thu Feb 21, 2008 11:26 pm
by pradip
I have used in query of the form:
"UPDATE userinfo SET lastlogin = NOW() WHERE uname='$username'";
with MySQL 5.0
Re: how to use NOW()
Posted: Thu Feb 21, 2008 11:30 pm
by alex.barylski
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.
Re: how to use NOW()
Posted: Fri Feb 22, 2008 12:05 am
by blacksnday
pradip wrote:I have used in query of the form:
"UPDATE userinfo SET lastlogin = NOW() WHERE uname='$username' ";
with MySQL 5.0
I know this doesnt answer your question But I had to add this:
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?
Re: how to use NOW()
Posted: Fri Feb 22, 2008 12:20 am
by alex.barylski
One thing you should add to that query at the end is: LIMIT 1
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.
Re: how to use NOW()
Posted: Fri Feb 22, 2008 11:57 am
by RobertGonzalez
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: