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";
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) );
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!