yiisoft/yii2 · error · yii\db\Exception

{} Failed to prepare SQL: $sql

Error message

{}
Failed to prepare SQL: $sql

What it means

Error "{} Failed to prepare SQL: $sql" thrown in yiisoft/yii2.

Source

Thrown at framework/db/Command.php:273

        }

        if ($this->db->getTransaction()) {
            // master is in a transaction. use the same connection.
            $forRead = false;
        }
        if ($forRead || $forRead === null && $this->db->getSchema()->isReadQuery($sql)) {
            $pdo = $this->db->getSlavePdo(true);
        } else {
            $pdo = $this->db->getMasterPdo();
        }

        try {
            $this->pdoStatement = $pdo->prepare($sql);
            $this->bindPendingParams();
        } catch (\Exception $e) {
            $message = $e->getMessage() . "\nFailed to prepare SQL: $sql";
            $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
            throw new Exception($message, $errorInfo, $e->getCode(), $e);
        } catch (\Throwable $e) {
            $message = $e->getMessage() . "\nFailed to prepare SQL: $sql";
            throw new Exception($message, null, $e->getCode(), $e);
        }
    }

    /**
     * Cancels the execution of the SQL statement.
     * This method mainly sets [[pdoStatement]] to be null.
     */
    public function cancel()
    {
        $this->pdoStatement = null;
    }

    /**
     * Binds a parameter to the SQL statement to be executed.
     * @param string|int $name parameter identifier. For a prepared statement

View on GitHub (pinned to 66f00d18a2)

When it happens

Trigger: Thrown at framework/db/Command.php:273 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of yiisoft/yii2@66f00d18a2 (2026-08-17). Data as JSON: /api/errors/9549ed2c154c35fa. Report an issue: GitHub.