2

My array date format ($fanniemaeDate) that I am passing to the database table column (date) is 11/19/2013 and I am trying to convert it to the MySQL format 2013-11-19 in my PDO prepare statement, however it is giving me an error "SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens"

I'm just learning PDO so not sure what is wrong with my STR_TO_DATE format within the prepare statement, or if this is even the correct way to prepare the statement.

If I remove the STR_TO_DATE from the prepare statement it will pass the time and rate correctly with the date stored as 0000-00-00

I need to know the correct way to write the prepare statement.

//PDO insert prepare statement for fannie_90dayrate table
$insertIrate = $pdo->prepare("INSERT INTO fannie_90dayrate (date, time, 90dayrate) 
VALUES (STR_TO_DATE(':fanniemaeDate', '%m/%d/%Y'), :fanniemaeTime, :fanniemaeRate)");  

//PDO execute statement
$insertIrate->execute(array(
':fanniemaeDate' => $fanniemaeDate['Fannie Mae Date'],
':fanniemaeTime' => $fanniemaeTime['Fannie Mae Time'],
':fanniemaeRate' => $fanniemaeRate['Fannie Mae Rate']));

1 Answer 1

1

Remove the quotes around :fanniemaeDate in

... STR_TO_DATE(':fanniemaeDate', '%m/%d/%Y') ...

Placing quotes around a parameter placeholder will cause it to be ignored, thus unbalancing your number of placeholders / number of parameters.

Side note...

I would recommend working with actual DateTime objects in PHP instead of date strings. That way, you can switch formats around quite easily, for example

$fanniemaeDate['Fannie Mae Date'] = DateTime::createFromFormat('m/d/Y', '11/19/2013');

// snip

$insertIrate->execute(array(
    ':fanniemaeDate' => $fanniemaeDate['Fannie Mae Date']->format('Y-m-d'),
    // etc

This way, you avoid having to convert the date string in the query.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.