PDO和MS SQL Server:为整数列插入值会导致错误 [英] PDO & MS SQL Server: inserting value for integer column results in error
问题描述
我试图在我的INT列中插入一个整数值.但是,当我执行以下PHP代码时,会导致错误告诉我我正在尝试插入文本",而事实并非如此……
I am trying to insert an integer value into my INT column. However, when I execute the following PHP-code, it results in an error telling me that I am trying to insert a "text", which should not be the case...
PHP
$myCode = 1;
$sth = $db->prepare("INSERT INTO tblCodes (myCode) VALUES (:myCode)");
$sth->bindParam(':myCode', $myCode, PDO::PARAM_INT);
$sth->execute();
错误
SQLSTATE [22018]:强制转换规范的字符值:206 [FreeTDS] [SQL Server]操作符类型冲突:文本与int不兼容 (SQLExecute [206]位于 /builddir/build/BUILD/php-5.6.9/ext/pdo_odbc/odbc_stmt.c:254)
SQLSTATE[22018]: Invalid character value for cast specification: 206 [FreeTDS][SQL Server]Operand type clash: text is incompatible with int (SQLExecute[206] at /builddir/build/BUILD/php-5.6.9/ext/pdo_odbc/odbc_stmt.c:254)
有人可以帮助我吗? 非常感谢!
Can anybody help me? Thanks a lot!
推荐答案
我认为这是一个错误.我通过更改查询解决了我的问题.根据您的情况,更改查询,如下所示:
I think that it is a bug. I solved my problem by change in the query. In your case change the query as below:
$myCode = 1;
$sth = $db->prepare("INSERT INTO tblCodes (myCode) VALUES (CAST(CAST(:myCode AS varchar) AS INTEGER))");
$sth->bindParam(':myCode', $myCode, PDO::PARAM_INT);
$sth->execute();
现在,它应该同时由PDO::PARAM_INT
和PDO::PARAM_STR
使用.
Now it should be worked by both PDO::PARAM_INT
and PDO::PARAM_STR
.
这篇关于PDO和MS SQL Server:为整数列插入值会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!