GatewayWorker icon indicating copy to clipboard operation
GatewayWorker copied to clipboard

DbConnection类在php8中数字占位报错 SQLSTATE[HY093]: Invalid parameter number

Open ttlxihuan opened this issue 2 years ago • 2 comments

生产环境使用php-8.1.2时数字序号占位绑定报错,经排查调整需要在 DbConnection::execute 方法下进行类型转换,并且catch块处理有点问题,PDOException::$errorInfo 只有一个元素。

if ($parameters[0][0] !== ':') { $parameters[0] = intval($parameters[0]); }

完整代码: /** * 执行 * * @param string $query * @param string $parameters * @throws PDOException */ protected function execute($query, $parameters = "") { try { $this->sQuery = @$this->pdo->prepare($query); $this->bindMore($parameters); if (!empty($this->parameters)) { foreach ($this->parameters as $param) { $parameters = explode("\x7F", $param); if ($parameters[0][0] !== ':') { $parameters[0] = intval($parameters[0]); } $this->sQuery->bindParam($parameters[0], $parameters[1]); } } $this->success = $this->sQuery->execute(); } catch (PDOException $e) { // 服务端断开时重连一次 if (isset($e->errorInfo[1]) && ($e->errorInfo[1] == 2006 || $e->errorInfo[1] == 2013)) { $this->closeConnection(); $this->connect();

            try {
                $this->sQuery = $this->pdo->prepare($query);
                $this->bindMore($parameters);
                if (!empty($this->parameters)) {
                    foreach ($this->parameters as $param) {
                        $parameters = explode("\x7F", $param);
                        $this->sQuery->bindParam($parameters[0], $parameters[1]);
                    }
                }
                $this->success = $this->sQuery->execute();
            } catch (PDOException $ex) {
                $this->rollBackTrans();
                throw $ex;
            }
        } else {
            $this->rollBackTrans();
            $msg = $e->getMessage();
            $err_msg = "SQL:".$this->lastSQL()." ".$msg;
            $exception = new \PDOException($err_msg, (int)$e->getCode());
            throw $exception;
        }
    }
    $this->parameters = array();
}

ttlxihuan avatar Mar 03 '22 06:03 ttlxihuan

这个类不维护了,因为有些老项目还在用所以没后删除。如果你还想用这个Db类可以发个PR我这边合并。

walkor avatar Mar 03 '22 07:03 walkor

我也是老项目在使用,最近升级PHP后就出问题了,我已经发了PR,

ttlxihuan avatar Mar 03 '22 10:03 ttlxihuan