Home > Backend Development > PHP Tutorial > How to Solve the PDO Exception: \'SQLSTATE[HY093]: Invalid Parameter Number\'?

How to Solve the PDO Exception: \'SQLSTATE[HY093]: Invalid Parameter Number\'?

Susan Sarandon
Release: 2024-12-01 03:43:17
Original
964 people have browsed it

How to Solve the PDO Exception:

PDO Exception: "SQLSTATE[HY093]: Invalid Parameter Number" Solved

While attempting to execute a PDO prepared statement, you faced the error "SQLSTATE[HY093]: Invalid parameter number". Inspecting the provided code, it becomes apparent that you have multiple named parameters with the same name.

The issue lies in your usage of named parameters within the SQL statement and the corresponding execution call. Specifically, you've used the parameter ":hash" twice, which is not allowed in PDO prepared statements.

To resolve this error, modify your code as follows:

$sql = "INSERT INTO persist (user_id, hash, expire)
        VALUES (:user_id, :hash, :expire)
        ON DUPLICATE KEY UPDATE hash=:hash2";
Copy after login

In this updated statement, an additional named parameter ":hash2" is introduced to replace the second occurrence of ":hash".

Furthermore, the execute() call should be modified accordingly:

$stm->execute(
    array(":user_id" => $user_id, ":hash" => $hash, ":expire" => $future, ":hash2" => $hash)
);
Copy after login

By providing distinct named parameters, you avoid the "SQLSTATE[HY093]" error caused by parameter redundancy. This approach ensures proper parameter binding and successful execution of the prepared statement.

The above is the detailed content of How to Solve the PDO Exception: \'SQLSTATE[HY093]: Invalid Parameter Number\'?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template