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

I've had an ongoing issue with inserting data with single quotes into MySQL. I use bind parameters and the $dbh->quote function, but to no avail. The query craps out. This has been a problem for years. Is there a work around for this?
  • Comment on Escaping Single Quote using Bind and $dbh->quote

Replies are listed 'Best First'.
Re: Escaping Single Quote using Bind and $dbh->quote
by moritz (Cardinal) on Apr 04, 2011 at 20:04 UTC
Re: Escaping Single Quote using Bind and $dbh->quote
by wind (Priest) on Apr 04, 2011 at 20:04 UTC

    What craps out? Where is your code?

    My preferred way of dealing with escaping is to always use placeholders

    my $sth = $dbh->prepare(q{INSERT INTO sales (product_code, qty, price) + VALUES (?, ?, ?)}; $sth->execute($product, $qty, $price) or die $dbh->errstr;

      Another reason for using place holders is to avoid SQL injection via something like Bobby tables.

        “Bobby Tables” ...

        ROTFLMAO ... :-D

Re: Escaping Single Quote using Bind and $dbh->quote
by locked_user sundialsvc4 (Abbot) on Apr 05, 2011 at 00:29 UTC

    As far as I am aware, if you use bind-parameters (which are unquoted question-marks), and provide a corresponding number of replacement values, say, to execute, then the SQL handler never cares about what those replacement values contain.   (The SQL statement parser never sees the values.)

    The following comment in the DBI documentation for the quote method also gives me pause:

    The quote() method should not be used with "Placeholders and Bind Values".

    In short, I suspect that the code “that has been giving you a problem for years” isn’t correct.   Could you please give a short, specific example of what you are doing now?   (The SQL string, the prepare and the execute.)