in reply to Re: Escaping a string
in thread Escaping a string

I'm doing from within perl - I define and declare the string in perl, and send the result to mysql. Plus, how do I convert "D\'ith" to "D'ith". Thank you.

Replies are listed 'Best First'.
Re: Re: Re: Escaping a string
by dga (Hermit) on Mar 13, 2003 at 20:54 UTC

    If you are using DBI to access MySQL then something like this may be better.

    use DBI; my $dbh=DBI->connect('DSN info here'); my $string="D'ith"; my $val_to_put_in_mysql=$dbh->quote($string);

    Or if you are using placeholders in your query then DBI will do the quote on insert for you. Either way DBI will fetch it back as D'ith for you.

: Re: Escaping a string
by ScooterQ (Pilgrim) on Mar 13, 2003 at 21:03 UTC
    From the DBI docs:
    use DBI; $quoted_string = $dbh->quote($string);
    If my understanding of black magic is correct that should take care of your quoting on the way into the DB. You should be able to select, etc. at will and have it come back out of the DB in its original (unescaped) form.
      THanks, out of interest, how would I escape using a regex.

        Frankly you got a bunch of valid answers here (zaxo for instance answered your question, but you didnt seem to grok it.) Around here we usually like to see people make some effort in their questions. Some kind of sign that we the respondants arent putting more effort into things than the questioner. Anyway,

        s/(\\\\|\\')|'/$1 ? $1 : "\\'"/ge;

        will do something like what you want. Its so confusing and strange looking because it doesnt screw up preescaped data (assuming that \\ is really a \). Theres probably cleaner nicer looking ways to do it to, but this works and came off the top of my head.

        Oh and please dont ask what/how it works. Read perlre like the rest of us did.


        ---
        demerphq