Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

[Solved] Mysql DBI placeholders not working

by tunafish (Beadle)
on Mar 17, 2015 at 03:16 UTC ( #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 (Bishop) 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
Node Status?
node history
Node Type: perlquestion [id://1120255]
Approved by NetWallah
Front-paged by NetWallah
help
Chatterbox?
and !@monks...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2017-12-17 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (464 votes). Check out past polls.

    Notices?