in reply to SQL statement produces no results when executed using DBI's prepared statement, but one record when executed directly in MySQL. WHY?

ted.byers:

Your SQL statements aren't actually formed properly, because you're using double-quoting and there's an unescaped '%' inside the text. So your SQL probably turned out to be something like:

select ... where ... OR mdf_20 LIKE '_a4bbf326102e3064a8d9e06558c4b5e2020ee353HASH(0x826998 +)' ^^^^^^^^^^^^^ +^

Your prepared statements have a different problem: you don't need to have quote characters in your arguments. It's likely turning into something like:

select ... where ... OR mdf_20 LIKE '''_a4bbf326102e3064a8d9e06558c4b5e2020ee353HASH(0x8269 +98)''' ^^ ^^^^^^^^^^^ +^^^^^

So first, be sure that you don't use the @ or % character inside double-quotes without escaping them. Second, go ahead and work with the parameterized version of the code, as it'll be better in the long term, and will be no more trouble to get running than the hardcoded version. I tweaked your second listing to be:

$sql = "SELECT action_id FROM transactions LEFT JOIN actions ON transactions.transaction_id = actions.transaction_id LEFT JOIN merchants ON merchant_id = idmerchants WHERE mdf_20 = ? AND action_type IN ('capture','sale') AND ABS(amount) = ABS(?) AND success = 1 AND processor_settlement_date IS NULL ORDER BY transaction_date;"; my $sth = $dbh->prepare($sql); $sth->bind_param(1, '1442a29888840b0b9505d34da6cd8897d153976d'); $sth->bind_param(2, 49.95); $sth->execute; my $aref = $sth->fetchrow_array if (defined $aref); print join(', ', @$aref),"\n"; $sth->bind_param(1, 'a4bbf326102e3064a8d9e06558c4b5e2020ee353'); $sth->bind_param(2, 4.95); $sth->execute; $aref = $sth->fetchrow_array; print join(', ', @$aref),"\n"; $sth->finish; my $like_cond = ''; $sql = "SELECT action_id,transaction_date FROM profitorius.actions WHERE transaction_id LIKE ? AND ABS(amount) = ABS(?) AND success = 1 AND action_type IN ('capture','sale') AND processor_settlement_date IS NULL ORDER BY transaction_date;"; $sth = $dbh->prepare($sql); $like_cond = '1442a29888840b0b9505d34da6cd8897d153976d%'; $sth->bind_param(1,$like_cond); $sth->bind_param(2, 49.95); $sth->execute; $aref = $sth->fetchrow_array; print join(', ', @$aref),"\n"; $like_cond = 'a4bbf326102e3064a8d9e06558c4b5e2020ee353%'; $sth->bind_param(1,$like_cond); $sth->bind_param(2, 4.95); $sth->execute; $aref = $sth->fetchrow_array; print join(', ', @$aref),"\n";

...roboticus

When your only tool is a hammer, all problems look like your thumb.

  • Comment on Re: SQL statement produces no results when executed using DBI's prepared statement, but one record when executed directly in MySQL. WHY?
  • Select or Download Code

Replies are listed 'Best First'.
Re: Reaped: SQL statement produces no results when executed using DBI's prepared statement, but one record when executed directly in MySQL. WHY?
by choroba (Cardinal) on Aug 07, 2014 at 06:23 UTC
    Besides replying to a to-be-reaped node, your comment is mistaken. Hashes aren't interpolated in double quotes (hash references in scalar variables are).
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      choroba:

      Regarding the hash interpolation: mea culpa. But as far as the to-be-reaped node part is considered, sometimes it takes me a while to compose a node. It certainly didn't look like it was about to be reaped before I started writing.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.