Jump to content

Recommended Posts

This taken from w3c schools fails

CREATE TABLE Orders
(
OrderId int NOT NULL,
ProductName varchar(50) NOT NULL,
OrderDate datetime NOT NULL DEFAULT NOW(),
PRIMARY KEY (OrderId)
)

 

Error:

#1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOW(),

PRIMARY KEY (OrderId)

)' at line 5

 

Can't understand this

Desmond.

:shrug:

Link to comment
https://forums.phpfreaks.com/topic/205922-now-dosnt-work/
Share on other sites

Ok timestamp and datetime in mysql are interchangebale Yes/No

No. DATETIME can store dates from 1000-01-01 00:00:00 to 9999-12-31 23:59:59, TIMESTAMP can store dates from 1970-01-01 00:00:00 to 2038-01-19 03:14:07

 

Can someone twll me of a default statment that will AUTOMATICALY enter the current date AND time please

 

I already did. I also provided you with a link to relevant page in manual.

Link to comment
https://forums.phpfreaks.com/topic/205922-now-dosnt-work/#findComment-1077940
Share on other sites

Sorry but this does not work

DROP TABLE IF EXISTS `datetest`;
CREATE TABLE IF NOT EXISTS `datetest` (
  `ID` int(11) NOT NULL auto_increment,
  `dbServer` datetime default CURRENT_TIMESTAMP,
  `webServer` datetime default NULL,
  PRIMARY KEY  (`ID`)
) TYPE=MyISAM PACK_KEYS=0;

Result:

#1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'CURRENT_TIMESTAMP,

  `webServer` datetime default NULL,

 

 

DROP TABLE IF EXISTS `datetest`;
CREATE TABLE IF NOT EXISTS `datetest` (
  `ID` int(11) NOT NULL auto_increment,
  `dbServer` TIMESTAMP default CURRENT_TIMESTAMP,
  `webServer` TIMESTAMP default NULL,
  PRIMARY KEY  (`ID`)
) TYPE=MyISAM PACK_KEYS=0;

Result:

#1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'CURRENT_TIMESTAMP,

  `webServer` TIMESTAMP default NULL,

 

 

Link to comment
https://forums.phpfreaks.com/topic/205922-now-dosnt-work/#findComment-1077964
Share on other sites

CREATE TABLE IF NOT EXISTS `datetest` (
  `ID` int(11) NOT NULL auto_increment,
  `dbServer` TIMESTAMP default CURRENT_TIMESTAMP,
  `webServer` TIMESTAMP,
  PRIMARY KEY  (`ID`)
) TYPE=MyISAM PACK_KEYS=0;

INSERT INTO datetest (webServer) VALUES (20100101123456);
SELECT * FROM datetest;

+----+---------------------+---------------------+
| ID | dbServer            | webServer           |
+----+---------------------+---------------------+
|  1 | 2010-06-27 23:39:27 | 2010-01-01 12:34:56 |
+----+---------------------+---------------------+
1 row in set (0.00 sec)

Link to comment
https://forums.phpfreaks.com/topic/205922-now-dosnt-work/#findComment-1077967
Share on other sites

No this does not work

#1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'CURRENT_TIMESTAMP,  `webServer` TIMESTAMP,  PRIMARY KEY  (`ID`)

 

Also NOW() works in microsoft SQL server and Oracle

Link to comment
https://forums.phpfreaks.com/topic/205922-now-dosnt-work/#findComment-1078117
Share on other sites

In MySQL 4.0, it's enough to declare a column as timestamp (as far as you have just one of those in your table).

It will be set to the current timestamp when you insert a row, just omit it in the INSERT statement.

Be aware that it will be updated when you update the row, unless you explicitly set it to its current value

Link to comment
https://forums.phpfreaks.com/topic/205922-now-dosnt-work/#findComment-1078677
Share on other sites

Thanks for all the help. I have managed to get MySql5 but I still need help with the insertion of the date and time.

DROP TABLE IF EXISTS `Monthly`;
CREATE TABLE IF NOT EXISTS `Monthly` (
  `MonthlyID` int(11) NOT NULL auto_increment,
  `Subject` varchar(255) default NULL,
  `Message` text,
  `Owner` tinyint,
  `DOE` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY  (`MonthlyID`)
) TYPE=MyISAM PACK_KEYS=0;


insert into Monthly VALUES ('','Subject','Message','1')

If the datetime is aouto inserted then this should work  ?

Link to comment
https://forums.phpfreaks.com/topic/205922-now-dosnt-work/#findComment-1079854
Share on other sites

Ok I understand this. In ANOTHER sql query I had an auto number id field and the query was

$sql = "INSERT INTO `EventLog` VALUES ('','" . $stamp . "','"  . $DateTime . "','" .$IP . "','" . $Page . "','" . $browser . "','" . $hit . "')";

I had to put  '', as the first to account for the auto ID.

 

in my example here I have a field that has to be auto inserted with the server time

`DOE` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

 

so how do I auto insert the time with or witout the extended sql VALUES()

 

Desmond.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/205922-now-dosnt-work/#findComment-1079880
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.