【问题标题】:PDO::PARAM_INPUT_OUTPUT not working in PostgreSQL?PDO::PARAM_INPUT_OUTPUT 在 PostgreSQL 中不起作用?
【发布时间】:2023-02-18 19:44:33
【问题描述】:

我无法让 PDO::PARAM_INPUT_OUTPUT 在 PostgreSQL 中工作。 PHP 8.1(来自 Sury 的最新 Debian),PostgreSQL 13.6。

程序声明:

CREATE OR REPLACE PROCEDURE public.procedure (
  a integer,
  inout b integer
)
AS
$body$
BEGIN
  b := a * b;
END;
$body$
LANGUAGE 'plpgsql'
SECURITY INVOKER;

在 SQL 中测试过程:

DO
$$
DECLARE b INT;
BEGIN
    b := 2;
    CALL public.procedure(3, b);
    RAISE NOTICE '%', b;
END
$$

它输出:

NOTICE:  6

在 PHP 中测试:

<?php
declare(strict_types=1);

$connection_params = [];
$connection_params[\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION;

$pdo = new \PDO(
    'pgsql:user=user;password=password;dbname=somedb;host=127.0.0.1;port=5432',
    null,
    null,
    $connection_params
);

$sql = 'CALL public.procedure(?, ?)';
$stmt = $pdo->prepare($sql);

$a = 2;
$b = 3;

$stmt->bindParam(1, $a, \PDO::PARAM_INT, 10);
$stmt->bindParam(2, $b, \PDO::PARAM_INT | \PDO::PARAM_INPUT_OUTPUT, 10);

print "Values of bound parameters _before_ CALL:\n";
print "  1: {$a} 2: {$b}\n";

$stmt->execute();

print "Values of bound parameters _after_ CALL:\n";
print "  1: {$a} 2: {$b}\n";

但它输出:

Values of bound parameters _before_ CALL:
  1: 2 2: 3
Values of bound parameters _after_ CALL:
  1: 2 2: 3

它应该输出:

Values of bound parameters _before_ CALL:
  1: 2 2: 3
Values of bound parameters _after_ CALL:
  1: 2 2: 6

我究竟做错了什么?

【问题讨论】:

    标签: php postgresql pdo


    【解决方案1】:

    请参阅此 GitHub 错误报告页面: https://github.com/php/doc-en/issues/2309

    基本上,现在的解决方法是在 $stmt->execute() 之后添加 $stmt->fetch() 以使用您的 (IN)OUT 参数填充一个数组,并使用它来填充您的变量(您通常希望自动填充!)。

    这也意味着您可以使用 BindValue() 而不是 BindParam()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-16
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2015-11-21
      • 2016-07-06
      • 2013-01-03
      相关资源
      最近更新 更多