By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,470 Members | 1,068 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,470 IT Pros & Developers. It's quick & easy.

how to insert string into mysql contains double backslash

P: 17
Expand|Select|Wrap|Line Numbers
  1. $str="james//bond";
  2. $str= mysqli_real_escape_string($con,$str);
  3. //insert query
after successful insert in database it value shows as "james/bond"

how I can insert exact string? anyone pls help.
Jun 5 '15 #1
Share this Question
Share on Google+
14 Replies


P: 2
try "james"+@"\\Bond"; I think it should work fine.
Jun 5 '15 #2

computerfox
100+
P: 278
Try this?

Expand|Select|Wrap|Line Numbers
  1. $str="james\/\/bond";
  2. $succ=$mysqli->query($sql);
  3.  
Jun 6 '15 #3

Expert 100+
P: 1,039
That dos not happen here....

Expand|Select|Wrap|Line Numbers
  1. mysql> create table actor(name varchar(40));
  2. Query OK, 0 rows affected (0.40 sec)
  3.  
  4. mysql> insert into actor values ("James//Bond");
  5. Query OK, 1 row affected (0.07 sec)
  6.  
  7. mysql> select * from actor;
  8. +-------------+
  9. | name        |
  10. +-------------+
  11. | James//Bond |
  12. +-------------+
  13. 1 row in set (0.04 sec)
  14.  
  15. mysql>
Jun 6 '15 #4

Expert 100+
P: 1,039
oh wait, I see what I did wrong......

"James Bond" is not an actor..... ;)
Jun 6 '15 #5

computerfox
100+
P: 278
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.
Jun 6 '15 #6

Expert 100+
P: 1,039
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.         error_reporting(E_ALL);
  3.         $m = new mysqli("localhost","test","test","test");
  4.         $actor = "Roger//Moore";
  5.         $sql = "INSERT INTO actor VALUES ('$actor')";
  6.         $result = $m->query($sql);
  7.  
  8.         $sql = "SELECT * FROM actor";
  9.         if ($result = $m->query($sql)) {
  10.                 while ($obj = $result->fetch_object())
  11.                 {
  12.                         echo $obj->name."\n";
  13.                 }
  14.         }
  15.         $result->close();
  16. ?>
  17.  
running this (from the command-line)
Expand|Select|Wrap|Line Numbers
  1. luuk@opensuse:~/tmp> php actors.php
  2. James//Bond
  3. Roger//Moore
  4. luuk@opensuse:~/tmp>
  5.  
Jun 7 '15 #7

Expert 100+
P: 1,039
and also if I use real_escape_string
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.         error_reporting(E_ALL);
  3.         $m = new mysqli("localhost","test","test","test");
  4.         $actor = "Ursula//Andress";
  5.         $actor = $m->real_escape_string($actor);
  6.         $sql = "INSERT INTO actor VALUES ('$actor')";
  7.         $result = $m->query($sql);
  8.  
  9.         $sql = "SELECT * FROM actor";
  10.         if ($result = $m->query($sql)) {
  11.                 while ($obj = $result->fetch_object())
  12.                 {
  13.                         echo $obj->name."\n";
  14.                 }
  15.         }
  16.         $result->close();
  17. ?>
  18.  
running this:
Expand|Select|Wrap|Line Numbers
  1. luuk@opensuse:~/tmp> php actors.php
  2. James//Bond
  3. Roger//Moore
  4. Ursula//Andress
  5. luuk@opensuse:~/tmp>
Jun 7 '15 #8

computerfox
100+
P: 278
harintfs, were you able to get it to work?
Jun 7 '15 #9

Expert 100+
P: 1,039
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
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.         echo "A\\B \n";
  3.         echo "A//B \n";
  4.         echo "A\B \n";
  5.         echo "A/B \n";
  6. ?>
  7.  
will print:
Expand|Select|Wrap|Line Numbers
  1. A\B
  2. A//B
  3. A\B
  4. A/B
  5.  
Jun 7 '15 #10

computerfox
100+
P: 278
@Luuk, if you look at his code, which I'm sure you did, he just has slashes.
He never mentions "backslashes" 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 "backslashes" thanks.
Jun 7 '15 #11

Expert 100+
P: 1,039
@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
Expand|Select|Wrap|Line Numbers
  1. echo "A\\B";
which produces
Expand|Select|Wrap|Line Numbers
  1. A\B
so, one should not just read question, but also read title
Jun 7 '15 #12

computerfox
100+
P: 278
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.
Jun 7 '15 #13

P: 1
but how about to inserting a location file in variable from "LoadFromFile" in MySQL?
2 Weeks Ago #14

P: 9
You need to escape your backslash :

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO gender
  2. (sex, date) VALUES (
  3. 'male are allowed \\ female are not allowed',
  4. "2012-10-06")
2 Weeks Ago #15

Post your reply

Sign in to post your reply or Sign up for a free account.