Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP-PDO_PGSQL - SQLSTATE[HY093]: Invalid parameter number

It's all the day that I'm stuck with this simple prepared statement:

// $conn it's my PDO Object
// and $intervention my params'array

$s = $conn->prepare("INSERT INTO intervention(firm_id,category,subject,amount,start_date,end_date) VALUES(:firm_id,':category',':subject',:amount,':start_date',':end_date')");
$result = $s->execute(array(
    'firm_id' => $firm_id ,
    'category' => $intervention["category"] ,
    'subject' => $intervention["subject"] ,
    'amount'=> $intervention["amount"] ,
    'start_date'=> $intervention["start_date"],
    'end_date'=>$intervention["end_date"] 
));

The execute will give me:

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: :category

Can someone help me understand what is wrong with this simple code?

like image 589
Luca Cerini Avatar asked Feb 18 '26 18:02

Luca Cerini


2 Answers

In this part of the query: VALUES(:firm_id,':category',

:category is taken as a literal string and not as a parameter name, because of the quotes enclosing it.

There should be no quotes around parameter names, as in:

...VALUES(:firm_id, :category,...

There is the same mistake for the other non-numeric parameters of the rest of the query.

like image 58
Daniel Vérité Avatar answered Feb 21 '26 13:02

Daniel Vérité


Parameters name should not have a quotes. The prepared statement will do the replacement properly. Pay attention too at the number of parameters you write in the query and what will you bind on execute method.

like image 39
Murilo Azevedo Avatar answered Feb 21 '26 15:02

Murilo Azevedo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!