walkor / walkor/GatewayWorker

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

Open
#88 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
1k
Forks
291
PR merge metrics
No merged PRs in 30d

Description

生产环境使用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();
}

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the DbConnection::execute method shown in the issue and trace how numeric placeholders reach PDOStatement::bindParam under PHP 8.1. Check both the initial execution path and the reconnect retry path, then verify that PDOException handling remains safe when errorInfo has only one element and that numeric placeholders execute successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.