Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Perl DBI escaping values retrieved from MySQL?

I have a value in MySQL that contains an apostrophe () and an ellipsis (...):

$ /bin/echo "select alias from url_alias where source = 'node/12024'" | \
  mysql --skip-column-names -D cat36ia_d7prod

Output:

forum/technical-discussion/nagging-questions-i’ve-been-too-embarrassed-ask…

When I retrieve the value with Perl DBI and DBD::mysql, the value has been changed:

$ perl -MDBI -MDBD::mysql -e
      '$dbh=DBI->connect( "DBI:mysql:database=my_db",nick );
       $v=$dbh->selectrow_array(qq|select alias from url_alias where source = "'node/12024'"|);
       print "$v\n";'

Output:

forum/technical-discussion/nagging-questions-i?ve-been-too-embarrassed-ask?

Why is Perl doing this? Can I override it?

like image 810
Channel Islander Avatar asked Dec 19 '22 07:12

Channel Islander


1 Answers

  1. Tell Perl how to encode the output.

    use open ':std', ':encoding(UTF-8)';
    
  2. Get the data from the database as text by using

    DBI->connect("DBI:mysql:database=my_db", $user, $pass, {
       mysql_enable_utf8 => 1,
    })
    
like image 98
ikegami Avatar answered Jan 06 '23 11:01

ikegami