how to insert string into mysql contains double backslash

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harintfs
    New Member
    • Dec 2011
    • 17

    how to insert string into mysql contains double backslash

    Code:
    $str="james//bond";
    $str= mysqli_real_escape_string($con,$str);
    //insert query
    after successful insert in database it value shows as "james/bond"

    how I can insert exact string? anyone pls help.
    Last edited by Rabbit; Jun 6 '15, 08:53 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • ankit20071991
    New Member
    • Jun 2015
    • 2

    #2
    try "james"+@"\\Bon d"; I think it should work fine.

    Comment

    • computerfox
      Contributor
      • Mar 2010
      • 276

      #3
      Try this?

      Code:
      $str="james\/\/bond";
      $succ=$mysqli->query($sql);

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        That dos not happen here....

        Code:
        mysql> create table actor(name varchar(40));
        Query OK, 0 rows affected (0.40 sec)
        
        mysql> insert into actor values ("James//Bond");
        Query OK, 1 row affected (0.07 sec)
        
        mysql> select * from actor;
        +-------------+
        | name        |
        +-------------+
        | James//Bond |
        +-------------+
        1 row in set (0.04 sec)
        
        mysql>

        Comment

        • Luuk
          Recognized Expert Top Contributor
          • Mar 2012
          • 1043

          #5
          oh wait, I see what I did wrong......

          "James Bond" is not an actor..... ;)

          Comment

          • computerfox
            Contributor
            • Mar 2010
            • 276

            #6
            Yeah... you also didn't do it in PHP, which doesn't answer his question. Try running the same querying in PHP and you will get the same result as the OP.

            Comment

            • Luuk
              Recognized Expert Top Contributor
              • Mar 2012
              • 1043

              #7
              Code:
              <?php
                      error_reporting(E_ALL);
                      $m = new mysqli("localhost","test","test","test");
                      $actor = "Roger//Moore";
                      $sql = "INSERT INTO actor VALUES ('$actor')";
                      $result = $m->query($sql);
              
                      $sql = "SELECT * FROM actor";
                      if ($result = $m->query($sql)) {
                              while ($obj = $result->fetch_object())
                              {
                                      echo $obj->name."\n";
                              }
                      }
                      $result->close();
              ?>
              running this (from the command-line)
              Code:
              luuk@opensuse:~/tmp> php actors.php
              James//Bond
              Roger//Moore
              luuk@opensuse:~/tmp>

              Comment

              • Luuk
                Recognized Expert Top Contributor
                • Mar 2012
                • 1043

                #8
                and also if I use real_escape_str ing
                Code:
                <?php
                        error_reporting(E_ALL);
                        $m = new mysqli("localhost","test","test","test");
                        $actor = "Ursula//Andress";
                        $actor = $m->real_escape_string($actor);
                        $sql = "INSERT INTO actor VALUES ('$actor')";
                        $result = $m->query($sql);
                
                        $sql = "SELECT * FROM actor";
                        if ($result = $m->query($sql)) {
                                while ($obj = $result->fetch_object())
                                {
                                        echo $obj->name."\n";
                                }
                        }
                        $result->close();
                ?>
                running this:
                Code:
                luuk@opensuse:~/tmp> php actors.php
                James//Bond
                Roger//Moore
                Ursula//Andress
                luuk@opensuse:~/tmp>

                Comment

                • computerfox
                  Contributor
                  • Mar 2010
                  • 276

                  #9
                  harintfs, were you able to get it to work?

                  Comment

                  • Luuk
                    Recognized Expert Top Contributor
                    • Mar 2012
                    • 1043

                    #10
                    MUST I remind you, that the subject of this message is NOT the same as what is tried in the example ?

                    The subject is about 'backslashes' (a.k.a. '\')
                    The example is showing 'slashes' (a.k.a. '/')

                    Next code
                    Code:
                    <?php
                            echo "A\\B \n";
                            echo "A//B \n";
                            echo "A\B \n";
                            echo "A/B \n";
                    ?>
                    will print:
                    Code:
                    A\B
                    A//B
                    A\B
                    A/B

                    Comment

                    • computerfox
                      Contributor
                      • Mar 2010
                      • 276

                      #11
                      @Luuk, if you look at his code, which I'm sure you did, he just has slashes.
                      He never mentions "backslashe s" in his first post other than the title. Could it be that he misspoke ;-)

                      Please read the question instead of assuming the question from the title. Since you seem to know exactly what they are asking for, please show me in his first post where he mentions "backslashe s" thanks.

                      Comment

                      • Luuk
                        Recognized Expert Top Contributor
                        • Mar 2012
                        • 1043

                        #12
                        @computerfox: let's wait for harintfs and what he has to say about slashes and backslashes...

                        I was reading the question, not the title ,when I gave some working code.

                        But what harintfs describes is the same as what happens to my example
                        Code:
                        echo "A\\B";
                        which produces
                        Code:
                        A\B
                        so, one should not just read question, but also read title

                        Comment

                        • computerfox
                          Contributor
                          • Mar 2010
                          • 276

                          #13
                          Depends how you look at it. Usually if we're lucky to have an example, that's what we should go with, but I agree, let's wait for the OP to clarify what they wanted.

                          Comment

                          • sugusaraha
                            New Member
                            • Mar 2020
                            • 1

                            #14
                            but how about to inserting a location file in variable from "LoadFromFi le" in MySQL?

                            Comment

                            • lewish95
                              New Member
                              • Mar 2020
                              • 33

                              #15
                              You need to escape your backslash :

                              Code:
                              INSERT INTO gender
                              (sex, date) VALUES (
                              'male are allowed \\ female are not allowed',
                              "2012-10-06")
                              Last edited by gits; Mar 10 '20, 02:08 PM. Reason: added code tags

                              Comment

                              Working...