Mysql Query Insert And Getdate()

liunx

Guest
Hello, I am using a database for some of the items on my website. To insert new itmes I just to a query insert and put the fields and the data to go into the fields...<br /><br />One of my fields is for the date and time of the entry, I was told that there is a getdate() function that can be used when I do the query insert and that this will put the date and time the item was inserted in the database? <br /><br />Can someone please show me the string of cade or help me better understand where to place the getdate() in my insert query?<br /><br />Thanks<!--content-->
You would be better off just using $thetime = time(); and then putting $thetime into the field that stores the purchase date. time() returns a unix timestamp for the time right now and you can then format it as you wish when you recall it. <a href="http://uk2.php.net/time" target="_blank">http://uk2.php.net/time</a> explains more.<!--content-->
<!--quoteo(post=167658:date=Feb 27 2006, 12:33 PM:name=carbonize)--><div class='quotetop'>QUOTE(carbonize @ Feb 27 2006, 12:33 PM) <a href="http://www.totalchoicehosting.com/forums/index.php?act=findpost&pid=167658"><img src='http://www.totalchoicehosting.com/forums/style_images/1/post_snapback.gif' alt='*' border='0' /></a></div><div class='quotemain'><!--quotec-->You would be better off just using $thetime = time(); and then putting $thetime into the field that stores the purchase date. time() returns a unix timestamp for the time right now and you can then format it as you wish when you recall it. <a href="http://uk2.php.net/time" target="_blank">http://uk2.php.net/time</a> explains more.<!--QuoteEnd--></div><!--QuoteEEnd--><br /><br /><br />Thanks for the reply. <br /><br />I want to try and explain better what Im trying to do. <br /><br />I have a database that stores different pictures for a website. One of the fields in the database is called "posted" and it is of type datetime. When I enter pictures into the database I use this string:<br /><br />mysql_query("INSERT INTO mydatabase (media_type, description, thumbnail, name, file, path) VALUES('entry1', 'entry2', 'entry3', 'entry4', 'entry5', 'entry6' ) ") or die(mysql_error()); <br /><br />Now there is another field in the database, its in between the thumbnail and name fields and its called "posted" and is type datetime, when I don't enter anything to it I get 0000-00-00 00:00:00 for a value. What I want here is the date and time the entry was made to the database?<br /><br />I heard I should use getdate() and you say to use time(). Basically what could I make this string look like to get the date and time entered into the "posted" field?<br /><br /><br />Thank you very much<!--content-->
I'd switch from datetime to just int as then you can use the timestamp as I stated. It is always recommended to use timestamps as then you can deal with any time differences and format the date/time how you want when displaying.<br /><br />If you do want to use datetime then <a href="http://dev.mysql.com/doc/refman/5.0/en/datetime.html" target="_blank">http://dev.mysql.com/doc/refman/5.0/en/datetime.html</a> will enlighten you as to how to use it. According to that page you would use Now() in the query.<!--content-->
Thanks for the info, I guess I should ask one more question (probably a dumb question). If I do use the int and insert a time then can I still order the database by date, like display newest first and stuff?<!--content-->
Yes by either adding an ID field to each item and having it auto_increment then order them by the ID descending or order by the timestamp as newer ones will always be higher than older ones.<!--content-->
 
Back
Top