1

I am trying to add two simple strings and one date to a SQL Server using PDO in PHP. 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. (SQLExecute[8180] at /builddir/build/BUILD/php-5.6.9/ext/pdo_odbc/odbc_stmt.c:254)

Does anyone know what I'm doing wrong?

Thanks in advance!

digifrog
  • 77
  • 12
  • @chris85 No the keys doesn't need colons, but they can. Both ways works – Rizier123 May 29 '15 at 15:36
  • Thanks for noticing @chris85, I've removed the tag "MySQL", it does not apply. I am using SQL Server. – digifrog May 29 '15 at 15:39
  • Have you checked, that your column names and table names are correct? – Rizier123 May 29 '15 at 15:39
  • 3
    `Now()` is for mysql. https://msdn.microsoft.com/en-us/library/ms188383.aspx @Rizier123 thanks I don't use the binding and have seen conflicting threads on that. – chris85 May 29 '15 at 15:39
  • @Rizier123 Yes, I have checked. If I shouldn't be using NOW(), what should I use? Thanks. – digifrog May 29 '15 at 15:41
  • I guess you didn't follow the provided link...`GETDATE()` here's another thread on it. http://stackoverflow.com/questions/385042/sql-server-equivalent-of-mysqls-now – chris85 May 29 '15 at 15:42
  • No way! I have used GETDATE() from the start, but I switched as I wasn't sure. Apparently I was chasing the wrong bug. Thanks a lot @chris85 for your help and patience! ;-) If you add this answer, I will approve. – digifrog May 29 '15 at 15:49

1 Answers1

2

Now() is a MySQL function. GetDate() is the sql-server's equivalent. Here's their documentation on the function, https://msdn.microsoft.com/en-us/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);
chris85
  • 23,846
  • 7
  • 34
  • 51