-1

我们msgdate在我的表中取一个字段private_message。我们将数据类型定义为msgdateas datetime

当我们插入数据时,该字段msgdate存储0000-00-00 00:00:00的不是当前日期。

$Query = "INSERT INTO private_message (id, sender, msgdate) VALUES ($_POST['id'],$_POST['to'], now()";
$Res = mysql_query($Query);
4

2 回答 2

0

you sure this works ?

"INSERT INTO private_message (id, sender, msgdate) VALUES ($_POST['id'],$_POST['to'], now()"

or should it be :

INSERT INTO private_message (id, sender, msgdate) VALUES ($_POST['id'],$_POST['to'], now())"
于 2012-06-09T04:52:06.980 回答
0

just a parenthesis has been missed

INSERT INTO private_message (id, sender, msgdate) VALUES ($_POST['id'],$_POST['to'], **OneOfTheseFunctions**)

replace OneOfTheseFunctions with on of these three statements:

There are three ways to retrieve the current datetime in SQL SERVER.

CURRENT_TIMESTAMP, GETDATE(), {fn NOW()}

CURRENT_TIMESTAMP

CURRENT_TIMESTAMP is a nondeterministic function. Views and expressions that reference this column cannot be indexed. CURRENT_TIMESTAMP can be used to print the current date and time every time that the report is produced.

GETDATE()

GETDATE is a nondeterministic function. Views and expressions that reference this column cannot be indexed. GETDATE can be used to print the current date and time every time that the report is produced.

{fn Now()}

The {fn Now()} is an ODBC canonical function which can be used in T-SQL since the OLE DB provider for SQL Server supports them. {fn Now()} can be used to print the current date and time every time that the report is produced.

If you run following script in Query Analyzer. I will give you same results. If you see execution plan there is no performance difference. It is same for all the three select statement.

SELECT CURRENT_TIMESTAMP
GO
SELECT {fn NOW()}
GO
SELECT GETDATE()
GO

dont worry, all of them are equal in function and performance!

于 2012-06-09T04:55:14.533 回答