Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

[Solved] Mysql DBI placeholders not working

by tunafish (Beadle)
on Mar 17, 2015 at 03:16 UTC ( [id://1120255]=perlquestion: print w/replies, xml ) Need Help??

tunafish has asked for the wisdom of the Perl Monks concerning the following question:

Solution: Bind variables should be an ARRAY, not an ARRAY REFERENCE. Original post follows:

It's the most bizarre thing. I've no idea what is going wrong.

I have the following code:

my $sql='UPDATE products SET sku=? WHERE sku=?'; my $sql_values = [$sku, $old_sku]; $dbh->do($sql, undef, $sql_values);

This fails with the error:

DBD::mysql::db do failed: You have an error in your SQL syntax; check +the manual that corresponds to your MySQL server version for the righ +t syntax to use near '' at line 1 at ...

However, the following code works fine:

my $sql='UPDATE products SET sku="'.$sku.'" WHERE sku="'.$old_sku.'"'; $dbh->do($sql);

Of course, I want to use placeholders in my SQL. I've been staring at this for the past 4 hours and have no clue what's going on. I don't even know where to begin Googling. This is incredibly bizarre. Elsewhere in the code, placeholders are working just dandy.

Edited to Add: Wow, I'm a moron. The bind variables should be an array, not an array reference... and all it took for me to figure that out was RTFM. Apologies for wasting your time, dear monks. I'd delete this node if I could.

Replies are listed 'Best First'.
Re: Mysql DBI placeholders not working
by bitingduck (Chaplain) on Mar 17, 2015 at 05:27 UTC

    The bind variables should be an array, not an array reference.... Apologies for wasting your time, dear monks. I'd delete this node if I could.

    Not a waste of time at all, and it's more valuable to leave the post up than to delete (or even strike through). Any mistake you make will be be made by countless others who might be spared some time because your post remains up and appears in search results. And in reading through the new nodes it's nice to be reminded of things to watch out for in the future.

Re: Mysql DBI placeholders not working
by Anonymous Monk on Mar 17, 2015 at 03:42 UTC
Re: Mysql DBI placeholders not working
by Laurent_R (Canon) on Mar 17, 2015 at 07:10 UTC
    Don't delete it, people can learn from it. I saw your code and thought: "I did not know that an arratref could be used for binding variables. Or, perhaps this is the origin of the problem. Hmm, I have to look it up." It turned out that I did not need to look it up, because your update clarified it. Well, in brief, thank you, I learned something from your post. ++

    Je suis Charlie.
Re: Mysql DBI placeholders not working
by choroba (Cardinal) on Mar 17, 2015 at 10:05 UTC
    I'd delete this node if I could.
    You can add '[Solved]' to the title, though, if you want only the most curious ones to read the question.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      You can also add (Solved) or {Solved} Solved: :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1120255]
Approved by NetWallah
Front-paged by NetWallah
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-19 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found