One of the more tedious to work with PDO is that it says that some variables are missing
PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
... but does not tell which ones. Is there any solution to identify them? Eg
$sql = "SELECT id, name WHERE id = :id AND name like :search_tem";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':id', '1');
$stmt->execute(); // throws exception - "**search_term missing"
It's so obvious everyone needs something like this. But I can't a simple solution.
Because of my low reputation I can't comment directly. So imagine this as a reply to the Post of Ragen Dazs on Jan 19 '13 at 15:58. I know this Topic is a few days old, but if some one like me stumble up on this by a google search...
How ever, I had problems with the regular expression in the third last line. As you can see in this example, the expression matches also time values with 00:00:00 as time. So I suggest to use the regular expression from this example.
I also wanted to know if there are needless parameters. This is how I did it, it takes a SQL query like that one in the examples above and a parameter array (See php doc example #2 for PDOStatement::execute).
If some one want it to throw an exception if something is wrong, just replace "return $result;" with the following lines of code:
have fun.
Updated Answer
Having misread the question first time round, I can now understand what it is you are wanting. As far as I can see, this is not possible as the error messages are defined by the PDO Exception.
However, a workaround could be to write a wrapper class for the PDO, and check the the bound values and query yourself, then if there are missing values, throw your own exception. This is probably the best way of achieving this...
Original Answer
I disagree with this, adding this sort of functionality would be pointless. Consider this:
This is what your code would output if it was allowed! Clearly the above would not work as nothing has been written in the
LIKE
term.I can understand why you want this, as I too have forgotten (intentionally and unintentionally) to bind values in the past, however, there is actually no benefit as the query will simply not work if you do not bind the values.
You would be much better off building the query dynamically like so:
I would say that named placeholders were invented by PDO team especially for this purpose - to ease visual verification of the query and placeholders.
I have to agree, such a feature could be some sort of syntax error sugar, but honestly, I don't find it too useful. There are other errors that are much more puzzling (like
you have an error near ''
one) while to find missing placeholder is not quite a big deal.After some days searching about this subject, testing some stuff I found this solution - follow the code from
bind
method from my Database class:This code needs revision, if someone finds a bug or have improved something please share!