On Mon, 25 Nov 2002 15:16:17 +0000 Tony Bowden <[EMAIL PROTECTED]> wrote:
>
> Consider the following script:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> use DBI;
>
> my $dbh = DBI->connect('dbi:mysql:music', 'user', 'pass');
>
> my $insert = $dbh->prepare_cached(qq{
> INSERT INTO user (username, password)
> VALUES (?, ?)
> });
>
> my $delete = $dbh->prepare_cached(qq{
> DELETE FROM user WHERE username = ?
> });
>
> $insert->execute('foo', 'nine');
> $insert->execute('bar', 9);
>
> $delete->execute('foo');
> $delete->execute('bar');
>
> When I run this, everything is fine.
>
> However, if I reverse the two 'insert' lines, so that the one with the 9
> comes before the one with the 'nine', I get:
>
> DBD::mysql::st execute failed: Unknown column 'nine' in 'field list' at
> test-fail line 18.
>
> I'm assuming that the first time the statement is executed something is
> trying to work out whether the field is numeric or string, and seeing a
> number, assumes it's numeric. But then, every future insert fails, with
> a highly misleading error ...
>
> Took me quite a while to realise this wasn't a Class::DBI / Ima::DBI
> problem ...
DBI remembers the types of placeholders after the first use. Either
enclose the number in quotes to make it a string or call bind_param() with
a dummy value that can't get confused for a number before you call
execute() the first time.
--
Mac :})
** I normally forward private questions to the appropriate mail list. **
Ask Smarter: http://www.tuxedo.org/~esr/faqs/smart-questions.html
Give a hobbit a fish and he eats fish for a day.
Give a hobbit a ring and he eats fish for an age.