开发者

PDO FreeTDS error

I have just upgraded my server from 开发者_StackOverflowphp 5.2 to 5.3.3 and I am having a weird error in PDO (with DBLIB).

I am connecting to an SQL Server 2005. I am having The error appears in any statement that prefixes a parameter with the "N" letter (to tell SQL Server that it's a unicode string).

The query looks like this:

INSERT INTO IPs (IP, PosterId, Note, DateAdded, Status, IsClass)
VALUES (:ip, :posterid, N:note, GETUTCDATE(), :status, :isclass)

Used to work properly on the old setup, the new one throws an exception:

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

Seems to work properly if I remove the N character in front of the parameter. However, I have thousands of statements like that and it would be a pain o remove. I would rather find out why this happens and fix it.

Any idea how to make this work?

Thanks

Later edit: Thanks to stillstanding below, I found that using non named parameters (question mark) works. But it still leaves me with changing lots of statements.

So this works:

INSERT INTO IPs (IP, PosterId, Note, DateAdded, Status, IsClass)
VALUES (?, ?, N?, GETUTCDATE(), ?, ?)


INSERT INTO IPs (IP, PosterId, Note, DateAdded, Status, IsClass) VALUES (:ip, :posterid, N:note, GETUTCDATE(), :status, :isclass)

First off, when you use a prepared statement, PDO will normally look like. $stm->bindParam(':note',$note); $stm->execute();

when PDO looks at this parameter, it will normally run your parameter through a safe insert parser, such as mysql_escape_string() and will place single quotes around your value, so... N:note turns into N'somevalue' Normally when you insert tokens they are completely independent of any other special characters and padded with spaces or tabs. You might consider avoiding using named parameters, this might help, but it would seem kind of unusual to have an insert such as N'somevalue' unless you are telling PDO this is an integer value which it will avoid using single quotes when it inserts.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜