简体   繁体   English

PHP PDO与MS SQL Server的接口

[英]PHP PDO interface with MS SQL Server

I am having trouble with using PHP PDO interface with Microsoft SQL Server. 我无法在Microsoft SQL Server中使用PHP PDO接口。 The problem is with converting PHP number values to use in functions of MS SQL Server. 问题在于转换PHP数字值以在MS SQL Server函数中使用。 I use the following statement to delete certain records: 我使用以下语句删除某些记录:

$sql = "DELETE FROM table WHERE SUBSTRING(attribute, 1, ?) = ?";

I prepare and execute this statement with code (a little shrinked): 我准备并使用代码执行此语句(略有缩减):

$query = $pdo->prepare ($sql);
$query->execute (array (strlen ('Text'), 'Text')); 

But the query always fails. 但是查询总是失败。 The error is: 错误是:

SQLSTATE[42000]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Argument data type nvarchar is invalid for argument 3 of substring function. SQLSTATE [42000]:[Microsoft] [SQL Server Native Client 11.0] [SQL Server]参数数据类型nvarchar对于子字符串函数的参数3无效。

I am assuming the number from strlen is not parsed into a number, but I don't know how to fix this (except to manually add into the query). 我假设来自strlen的数字未解析为数字,但是我不知道如何解决(除非手动添加到查询中)。

I have found these links, however not very helpful. 我发现这些链接,但是不是很有帮助。

http://social.msdn.microsoft.com/Forums/en-US/sqldriverforphp/thread/0f09ac5e-62cd-4ccf-b2cb-848aad23811e http://social.msdn.microsoft.com/Forums/zh-CN/sqldriverforphp/thread/0f09ac5e-62cd-4ccf-b2cb-848aad23811e

http://drupal.org/node/1169202 http://drupal.org/node/1169202

The guys on Drupal had same error, but they fixed it with recreating function with casts. Drupal上的家伙有同样的错误,但是他们使用重铸功能重新创建了它。 Is there any other way to fix this? 还有其他解决方法吗?

Thanks. 谢谢。

Not sure if this will solve it, but try something like this 不知道这是否可以解决问题,请尝试类似的方法

$length = strlen('Text');
$text = "Text";

$sql = "DELETE FROM table WHERE SUBSTRING(attribute, 1, :len) = :text";
$query = $pdo->prepare ($sql);
$query->bindParam(':len', $length, PDO::PARAM_INT);
$query->bindParam(':text', $text, PDO::PARAM_STR);
$query->execute(); 

Try: 尝试:

$query = $pdo->prepare ($sql);
$query->execute (array ((int) strlen ('Text'), 'Text')); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM