DBI question: binding params in HAVING clauses

Matt Sergeant msergeant at messagelabs.com
Wed Jan 25 19:10:09 GMT 2006


On 25 Jan 2006, at 11:35, David Cantrell wrote:

> Using SQLite, if I execute this, with the value in the HAVING clause
> just there in plain text, it works:
>
>     SELECT COUNT(actors.name), countries.name
>       FROM actorsXcountries, countries, actors
>      WHERE actors.id = actorsXcountries.actorid AND
>            countries.id = actorsXcountries.countryid
>   GROUP BY countries.name
>     HAVING COUNT(actors.name) < 3
>   ORDER BY COUNT(actors.name)
>
> but if instead I replace the 3 with a placeholder, and supply it like
> so:
>
>   $sth = $dbh->prepare('...');
>   $sth->execute(3);
>
> then the HAVING clause seems to be ignored, and I later get too many
> rows back.
>
> Has anyone come across this before?

Yes, it depends on where the underlying DB driver supports 
placeholders. IIRC Pg is the same with LIMIT/OFFSET params (don't quote 
me on that).

>   And do you have a solution?

Use sprintf.

Matt.


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________


More information about the london.pm mailing list