AddSlashesh in Perl

Discussion in 'Programming' started by dnsman, Jan 20, 2011.

  1. #1
    hi everyone
     
    use DBI;
     $user='xxxx';
     $password='xxxx';
     $host='localhost';
     $db='test';
     $dbh = DBI->connect("DBI:mysql:database=$db;host=$host",
                          $user, $password, {RaiseError => 1});
     $name = "vrs'z'kf";
    
       $dbh->do("INSERT INTO test (test)VALUES('$name' )");
    Code (markup):
    Here is my code.
    How i can escape $name string for successfully mysql_query ? //OR// i found SQL syntax error;
    Thanks all!
     
    dnsman, Jan 20, 2011 IP
  2. nuare

    nuare Member

    Messages:
    49
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #2
    I found this in Google buy request Perl function equivalent of PHP’s AddSlashes()

    sub AddSlashes {
    $text = shift;
    ## Make sure to do the backslash first!
    $text =~ s/\\/\\\\/g;
    $text =~ s/'/\\'/g;
    $text =~ s/"/\\"/g;
    $text =~ s/\\0/\\\\0/g;
    return $text;
    }
     
    nuare, Jan 20, 2011 IP
  3. dnsman

    dnsman Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Big thanks
     
    dnsman, Jan 20, 2011 IP