PDOException HY093 and quotation marks

PDOException HY093 Invalid parameter number: parameter was not defined - it was the quotation marks' fault!

Return to blog
Posted: October 5, 2019 16:29; Updated: October 7, 2019 18:49

I was trying my first PHP PDO update query, when I kept getting the following error:

PDOException HY093 Invalid parameter number: parameter was not defined

This was my first time working with PDO I had done a lot of reading on various sites to help with my transition from mysqli. My original code looked with this:

$blogPostSql = 'UPDATE posts SET post_title = ":postTitle" WHERE post_id = :postID'
$blogPostQuery = $pdo->prepare($blogPostSql);
$blogPostQuery->bindValue(':postTitle', $postTitle, PDO::PARAM_STR);
$blogPostQuery->execute();

The error was always thrown on the bindValue() line and I struggled to figure out what it wasn't working. I kept reading 'parameter was not defined', so I kept trying to find out why either :postTitle or $postTitle were undefined.

It wasn't until I came across a notation on one PDO guide that stated that quotation marks around string named placeholders were not required did I guess that "not required" actually meant will throw an exception.

It was the quotation marks' fault! The proper line was without the quotation marks, as such:

$blogPostSql = 'UPDATE posts SET post_title = :postTitle WHERE post_id = :postID'