[英]“Syntax error or access violation” when inserting dates using PDO and SQL Server
I am trying to add two simple strings and one date to a SQL Server using PDO in PHP. 我正在尝试在PHP中使用PDO向SQL Server添加两个简单的字符串和一个日期。 I'm currently using the following code to do so:
我目前正在使用以下代码进行操作:
$data = array(
'Omschrijving' => 'Mijn mooie omschrijving...',
'Toelichting' => 'Mijn leuke toelichting...'
);
# Insert data
$STH = $DBH->prepare("INSERT INTO memo (Datum, Omschrijving, Toelichting) VALUES (NOW(), :Omschrijving, :Toelichting)");
$STH->execute($data);
It works perfectly without the date, but for some reason it gives me the following error when I try to add the date: 它没有日期就可以完美工作,但是由于某些原因,当我尝试添加日期时,它给了我以下错误:
SQLSTATE[42000]: Syntax error or access violation: 8180 [FreeTDS][SQL Server]Statement(s) could not be prepared.
SQLSTATE [42000]:语法错误或访问冲突:8180 [FreeTDS] [SQL Server]声明无法准备。 (SQLExecute[8180] at /builddir/build/BUILD/php-5.6.9/ext/pdo_odbc/odbc_stmt.c:254)
(位于/builddir/build/BUILD/php-5.6.9/ext/pdo_odbc/odbc_stmt.c:254的SQLExecute [8180])
Does anyone know what I'm doing wrong? 有人知道我在做什么错吗?
Thanks in advance! 提前致谢!
Now()
is a MySQL function. Now()
是一个MySQL函数。 GetDate()
is the sql-server's equivalent. GetDate()
是sql-server的等效项。 Here's their documentation on the function, https://msdn.microsoft.com/en-us/library/ms188383.aspx . 这是有关此功能的文档, https://msdn.microsoft.com/zh-cn/library/ms188383.aspx 。
So provided code should become: 因此提供的代码应变为:
$data = array(
'Omschrijving' => 'Mijn mooie omschrijving...',
'Toelichting' => 'Mijn leuke toelichting...'
);
# Insert data
$STH = $DBH->prepare("INSERT INTO memo (Datum, Omschrijving, Toelichting) VALUES (GETDATE(), :Omschrijving, :Toelichting)");
$STH->execute($data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.