Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ttlxihuan opened this issue Mar 3, 2022 · 2 comments

Comments

@ttlxihuan
Copy link
Contributor

生产环境使用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();
}
@walkor
Copy link
Owner

walkor commented Mar 3, 2022

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

@ttlxihuan
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants