Connecting Tech Pros Worldwide Forums | Help | Site Map

When should special characers be escaped inside strings?

Newbie
 
Join Date: Aug 2008
Posts: 12
#1: May 18 '09
Hi all. I picked up the following code example from the php manual:
Expand|Select|Wrap|Line Numbers
  1. $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
  2.     OR die(mysql_error());
  3.  
  4. // Query
  5. $query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
  6.             mysql_real_escape_string($user),
  7.             mysql_real_escape_string($password));
My question is, shouldn't we be escaping the ' in the sprintf statement with backslashes? Like this? -->

Expand|Select|Wrap|Line Numbers
  1. $query = sprintf("SELECT * FROM users WHERE user=\'%s\' AND password=\'%s\'",
  2.             mysql_real_escape_string($user),
  3.             mysql_real_escape_string($password));
I'm kind of confused with all this. When are we supposed to put the backslashes?? Please somebody help.

Also, while you pros are at it, I'll really appreciate it if you could tell me if these two strings are the same:

String A:
Expand|Select|Wrap|Line Numbers
  1. char A[] = "Hello. 'How are you' "
String B:
Expand|Select|Wrap|Line Numbers
  1. char B[] = "Hello. \'How are you\'"
Because both print Hello. 'How are you' on the screen on using printf.
Also, both have the same string lengths.

I'm wondering why we need to escape the 's at all if they print the same string and are of the same length as well.

Sorry if the question is uber-dumb.
Thanks in advance,
Sid

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,753
#2: May 18 '09

re: When should special characers be escaped inside strings?


Hi.

If you open a string using double-quotes, there is no need to escape single-quotes inside that string. (And the other way around.)

However, both will accept the escaped versions and convert them into their respective characters, just like they are supposed to.
Expand|Select|Wrap|Line Numbers
  1. // Both print exactly the same: John's name
  2. echo "John's name";
  3. echo "John\'s name";
  4.  
The only time you really need to escape a quote character is if it is within a string that it would otherwise close.
Like a single-quote inside a single-quoted string.
Expand|Select|Wrap|Line Numbers
  1. echo 'John's name'; // Gives a parse error
  2. echo  'John\'s name'; // Works
  3.  
  4. echo "John said: "What?""; // Gives a parse error
  5. echo "John said: \"What?\""; // Correct
  6.  
Does that answer you question?

Edit.
Looking closer at your bottom examples, they look like C/C++?
In PHP, both strings would print the single-quotes as if the escape characters weren't there. In C/C++ however, I do not know.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#3: May 18 '09

re: When should special characers be escaped inside strings?


When dealing with SQL, you should always escape special characters.
Newbie
 
Join Date: Aug 2008
Posts: 12
#4: May 20 '09

re: When should special characers be escaped inside strings?


Thank you atli and Markus. That helped. :)
-Sid
Reply

Tags
backslash, escape, escaping