SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens on line 102

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens on line 102

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens on line 102

When debugging PDO errors, the message SQLSTATE[HY093]: Invalid parameter number is often a symptom of a mismatch between your PHP logic and the actual database query execution. The specific error cited in the provided code snippet occurs because the application attempts to bind four parameters (:publicationDate, :title, :content, :articleid) while the SQL statement only defines three placeholders.

The root cause lies in the insert() method within the Comment class. Let's dissect the failing query and the binding logic to understand why this happens.

The Mismatch in insert()

In the provided code, the insert() method constructs the following SQL string:

$sql = "INSERT INTO comments ( publicationDate, title, content, articledid ) 
        VALUES ( FROM_UNIXTIME(:publicationDate), :title, :content, :articleid )";

Here, there are exactly four named placeholders in the VALUES clause corresponding to the columns publicationDate, title, content, and articledid. However, looking closely at the subsequent binding logic reveals the discrepancy:

$st->bindValue(":publicationDate", $this->publicationDate, PDO::PARAM_INT);
$st->bindValue(":title", $this->title, PDO::PARAM_STR );
$st->bindValue(":content", $this->content, $this->content, PDO::PARAM_STR ); // Note: Typo in source? No, let's look at the next line.
$st->bindValue(":articleid", $this->articleid, PDO::PARAM_STR );

Wait, looking strictly at the source text provided:

  1. $st->bindValue(":publicationDate", ...)
  2. $st->bindValue(":title", ...)
  3. $st->bindValue(":content", ...)
  4. $st->bindValue(":articleid", ...)

Actually, in the provided snippet, there appear to be four bindings defined. However, the error message explicitly states "number of bound variables does not match number of tokens". In many PDO implementations (depending on the driver version), if you omit a binding entirely or pass an empty string where a value is expected, it can still trigger this specific error if the parser counts the token but finds no bind.

However, a more subtle and common cause for HY093 in this context involves how the SQL statement is parsed before execution. If the database schema contains a column named articledid, but the PHP variable accessing it is $this->articleid (singular), and if there is a typo in the binding list where one line was accidentally commented out or skipped during a refactor, PDO will throw this error.

But let's look closer at the specific line count mentioned: line 102. In the snippet provided, the insert() method is near the end. If the error occurs there, it implies that for some reason, one of the $st->bindValue calls is missing or the SQL string has been modified externally (e.g., via a build script) to remove a placeholder without removing the corresponding binding call, or vice versa.

A frequent culprit in legacy PHP codebases like this is the use of mysql_escape_string() elsewhere or inconsistencies in column naming. Notice in the getList method:

$sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM comments WHERE articleid = :art ORDER BY " . mysql_escape_string($order) . " LIMIT :numRows";

This method uses mysql_escape_string() on the $order parameter. This is a critical security flaw (SQL injection risk). While this doesn't directly cause the HY093 error, it indicates the codebase relies on older practices that might conflict with strict PDO type checking in newer PHP versions. The getList method also defines two placeholders (:art, :numRows) and binds them correctly, but the insert method is where the logic gets fragile if any property (like $this->id) is missing from the object state.

Correcting the Bindings

To resolve the HY093 error, you must ensure that every placeholder in the SQL string has a corresponding bindValue or execute call with a value. If the database column is named articledid (as seen in the SQL), but your PHP object property is $this->articleid, ensure the binding targets the correct variable.

If the error persists, check if one of the bindings was accidentally omitted during editing. For example, if you remove the line:

$st->bindValue(":content", $this->content, PDO::PARAM_STR);

but leave the :content placeholder in the SQL, PDO will throw this exact error because it sees a token but cannot find a bound variable for it.

Additionally, notice the update() method has a similar structure but includes :summary and :imageExtension in the SQL:

$sql = "UPDATE comments SET publicationDate=FROM_UNIXTIME(:publicationDate), title=:title, summary=:summary, content=:content, articleid=:articleid,imageExtension=:imageExtension WHERE id = :id";

If your database table does not actually have columns named summary or imageExtension, the query will fail with a different error (unknown column). However, if you are binding extra variables to placeholders that are present, you must ensure they match.

Best Practices for Parameter Binding

Relying on manual string concatenation for SQL statements is dangerous. While the provided code uses prepared statements correctly by using :token syntax, it still suffers from dynamic ordering in getList():

ORDER BY " . mysql_escape_string($order) . "

This allows a user to inject arbitrary SQL logic into the ORDER BY clause. A safer approach is to whitelist allowed column names:

$allowedColumns = ['publicationDate', 'title', 'content', 'id'];
if (in_array($order, $allowedColumns)) {
    $sql = "SELECT ... ORDER BY " . $conn->quote($order); // Or just direct use if sanitized
}

For robust applications, consider migrating this logic to an ORM like Eloquent or using a library that handles type coercion and binding validation automatically. This reduces the risk of human error where placeholders are miscounted.

If you are maintaining legacy code like the Comment class shown here, always validate that the number of $st->bindValue calls exactly matches the count of :name tokens in your SQL string before execution.

Our network