in reply to Using placeholders, getting unexpected SQL errors

What DBD are you using?

From the DBI docs:

If any arguments are given, then "execute" will effec- tively call "bind_param" for each value before execut- ing the statement. Values bound in this way are usu- ally treated as "SQL_VARCHAR" types unless the driver can determine the correct type (which is rare), or unless "bind_param" (or "bind_param_inout") has already been used to specify the type.

So maybe the driver that you are using is guessing incorrectly as to the type of "2 89" and it treating it as an integer. If that is the case, it looks as if you might have stumbled upon a bug in the DBD.

Try doing a DBI->trace(2) (or higher up to level 9) to see what is going on inside the DBI/DBD, and if you are still stumped or if it looks like there might be a bug, post the information to dbi-users.

In the meantime you can probably explicity spell out the bind type of that placeholder using bind_param():

$insert_cmd = "INSERT INTO PhoneNumbers (cusID, adtID, pntID, phnPart1, phnPart2, phnPart3, phnExt) VALUES (?, ?, ?, ?, ?, ?, ?)"; $phone_insert = $gcssDBH->prepare($insert_cmd); $phone_insert->bind_param(6,undef,SQL_VARCHAR); #bind undef -- execute + will remember the bind type $phone_insert->execute( $row->{'cust_id'}, $adtID, $phoneNumberTypes->{'Day'}, $row->{'cust_phac'}, $row->{'cust_phpx'}, $row->{'cust_phsx'}, $row->{'cust_phex'} );

Replies are listed 'Best First'.
Re: Re: Using placeholders, getting unexpected SQL errors
by ok (Beadle) on Mar 14, 2003 at 20:46 UTC
    I am thoroughly freaked out. Problem solved, no idea why. Lemme explain... I did not include a piece of code in my post:
    if (($row->{'cust_phpx'} != '') && ($row->{'cust_phsx'} != '')) { # # Execute the query # }
    I took your advice and traced the bastard, which showed me how MySQL was choking (even after using bind_param), if you will:
    -> execute for DBD::mysql::st (DBI::st=HASH(0x8218138)~0x82180b4 ' +3048770' '2' '1' '704' 53 2 89 '45') -> dbd_st_execute for 082180a8 Binding parameters: INSERT INTO PhoneNumbers (cusID, adtID, pntI +D, phnPart1, phnPart2, phnPart3, phnExt) VALUES ('3048770', '2', '1', '704', 53, 2 89, '45') ERROR EVENT 1064 'You have an error in your SQL syntax near '89, ' +45')' at line 2' on DBI::st=HASH(0x82180b4)
    On a lark I replaced the "!=" in the if test with "ne". No more errors. I don't get it. It seems like the comparison operator "fiddled with the nature" of the variable it was operating on. Whatever that means. By the way check this out. See the fifth bugfix for v.053. (This has nothing to do with the code I'm working on, I don't even know what "WeSQL" is, but when I desperately googled for answers, I found this). Something smells fishy...I hope it's just my lunch.