Forays into PHP and MySQL

By | April 19, 2007

I’ve just started working extensively with PHP and MySQL.  Here’s a simple tip that took me a while to figure out (despite copious examples on, I couldn’t find this one:

My MySQL table has a  Timestamp (Z) field, used to assign the current date and time to it when a record is added. Using mySQL in PHP, I can assign the value to it like this:

$result=mysql_query(“INSERT INTO qNotices (nPostDateTime) VALUES(NOW()”);

or I can assign the value with a PHP assignment statement, using this:

  $nPostDateTime= date(‘YmdHis’);  // Same as NOW() MySQL function.
 $result=mysql_query(“INSERT INTO qNotices (nPostDateTime) VALUES($nPostDateTime”);

Note that you do not need any dashes or semi-colons in the format string here, even though the field looks like this :  2007-04-19 10:23:32 when you view the data with a SELECT statement.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.