PHP PDO bindParam/bindValue 多次

2021-12-26 00:00:00 php pdo sqlbindparameter

我在使用 ODBC 的 PDO 语句时遇到问题.

I'm having an issue with the PDO statements for ODBC.

我在 Windows Server 2003 和 PHP 5.4.x 中使用 SQL SERVER 7

I'm using SQL SERVER 7 in Windows Server 2003 and PHP 5.4.x

例如:

我有查询:

(这不是实际的查询,但它适用于示例)

(this is not the actual query but it serves right for the example)

$query = SELECT * FROM table WHERE number = :number OR number = :number

在我的 php 中:

$conn = new PDO($connectionString);

$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$statement = $conn->prepare($query);

$statement->bindParam(':number', $someNumber);

$statement->execute();

这会引发错误

COUNT field incorrect or syntax error

问题是,bindParam 只绑定了 :number 的第一次出现......并且再次尝试绑定它也不起作用.

The thing is, bindParam is only binding the FIRST occurrence of :number ... AND trying to bind it again doesn't work either.

有没有办法绑定多个同名的命名参数?

Is there a way to bind multiple named params with the same name?

我试图不使用位置参数使用 ?代替

I'm trying not to use positional params using the ? instead

推荐答案

理论上你可以打开准备语句的模拟.

Theoretical you could turn on emulation of prepared statements.

您必须为您希望的每个值包含一个唯一的参数标记当您调用 PDOStatement::execute() 时传入语句.你不能多次使用同名的命名参数标记准备好的语句,除非启用了仿真模式.

You must include a unique parameter marker for each value you wish to pass in to the statement when you call PDOStatement::execute(). You cannot use a named parameter marker of the same name more than once in a prepared statement, unless emulation mode is on.

http://www.php.net/manual/en/pdo.准备.php

相关文章