473,382 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

PHP, mysql, and escaping characters

Hi everyone,

I'm having a bit of trouble understanding the purpose of escaping
nulls, and the use of addcslashes.

Firstly, the manual states that:

"Strictly speaking, MySQL requires only that backslash and the quote
character used to quote the string in the query be escaped. This
function quotes the other characters to make them easier to read in
log files"

While http://dev.mysql.com/doc/refman/5.0/...ng-syntax.html shows
that NULLs must be escaped as well as quotes and backslashes (if
inserting into BLOBs), which seems in contradiction to the statement
above.

In addition to the qutoes, backslashes, and nulls, real escape string
also escapes "\n, \r, and \x1a.

Meanwhile, the article at http://www.zend.com/zend/trick/tricks-
sept-2001.php?article=tricks-sept-2001&kind=tr&id=12886&open=1&anc=0

states that:

"Inserting a large value into a BLOB column is no different than
inserting any other character data. However, the image likely includes
a few characters that have special meaning in SQL or to the MySQL
driver. The addslashes function inserts backslashes before characters
with special meanings in SQL, such as single-quotes.

I used the addcslashes function to replace NUL characters with a \0
code because MySQL treats this character as the end of a string. This
is normal behavior for the C language, but not PHP. Otherwise, loading
the image into a variable would have required more steps."

I conducted an experiment using mysql 5.1 and tried inserting some
binary strings/data into a BLOB field. I tried no escaping, escaping
using addcslashes($string,"\0"), and mysql_real_escape_string, wrote
the escaped string out to file, and then inserted the data into the
database using the escaped string in an insert query.

The original data was as follows:

00 61 00 62 00 63 00 64

This was not escaped, and used in the insert query as is. Surprisingly
(because of what I had read previously), the data was inserted without
any complaints from mysql and appeared in the DB correctly. Why has it
worked, when the SQL manual and the zend website said that NULLs MUST
be escaped??

Next I tried the addcslashes:

The string that was inserted into the query was (ie: the result of
calling addcslashes($string,"\0"))
5C 30 30 30 61 5C 30 30 30 62 5C 30 30 30 63 5C 30 30 30 64

Whereas the data inserted into the database was:
00 30 30 61 00 30 30 62 00 30 30 63 00 30 30 64

For some reason addcslashes has, for every byte in the original data,
replaced it with a backslash (x5C) followed by three spaces (x30).
This results in the mangled data appearing in the database. I'm not
quite sure what the guy from zend was doing, or what he means by "This
is normal behavior for the C language, but not PHP. Otherwise, loading
the image into a variable would have required more steps."???

Finally, I tried escaping the data with mysql_real_escape_strings

The string that was inserted into the query was:
5C 30 61 5C 30 62 5C 30 63 5C 30 64

Which is what I expected addcslashes to give..

The data was inserted into the DB correctly.

So this is working as I expected, but why even bother escaping the
nulls if, as shown by the first experiment, it doesn't seem to be
needed?

Having said this, why would you want to escape the other characters:
\n, \r, and \x1a??

Thanks

Taras

Feb 7 '07 #1
3 5353
Taras_96 wrote:
Hi everyone,

I'm having a bit of trouble understanding the purpose of escaping
nulls, and the use of addcslashes.

Firstly, the manual states that:

"Strictly speaking, MySQL requires only that backslash and the quote
character used to quote the string in the query be escaped. This
function quotes the other characters to make them easier to read in
log files"

While http://dev.mysql.com/doc/refman/5.0/...ng-syntax.html shows
that NULLs must be escaped as well as quotes and backslashes (if
inserting into BLOBs), which seems in contradiction to the statement
above.

In addition to the qutoes, backslashes, and nulls, real escape string
also escapes "\n, \r, and \x1a.

Meanwhile, the article at http://www.zend.com/zend/trick/tricks-
sept-2001.php?article=tricks-sept-2001&kind=tr&id=12886&open=1&anc=0

states that:

"Inserting a large value into a BLOB column is no different than
inserting any other character data. However, the image likely includes
a few characters that have special meaning in SQL or to the MySQL
driver. The addslashes function inserts backslashes before characters
with special meanings in SQL, such as single-quotes.

I used the addcslashes function to replace NUL characters with a \0
code because MySQL treats this character as the end of a string. This
is normal behavior for the C language, but not PHP. Otherwise, loading
the image into a variable would have required more steps."

I conducted an experiment using mysql 5.1 and tried inserting some
binary strings/data into a BLOB field. I tried no escaping, escaping
using addcslashes($string,"\0"), and mysql_real_escape_string, wrote
the escaped string out to file, and then inserted the data into the
database using the escaped string in an insert query.

The original data was as follows:

00 61 00 62 00 63 00 64

This was not escaped, and used in the insert query as is. Surprisingly
(because of what I had read previously), the data was inserted without
any complaints from mysql and appeared in the DB correctly. Why has it
worked, when the SQL manual and the zend website said that NULLs MUST
be escaped??

Next I tried the addcslashes:

The string that was inserted into the query was (ie: the result of
calling addcslashes($string,"\0"))
5C 30 30 30 61 5C 30 30 30 62 5C 30 30 30 63 5C 30 30 30 64

Whereas the data inserted into the database was:
00 30 30 61 00 30 30 62 00 30 30 63 00 30 30 64

For some reason addcslashes has, for every byte in the original data,
replaced it with a backslash (x5C) followed by three spaces (x30).
This results in the mangled data appearing in the database. I'm not
quite sure what the guy from zend was doing, or what he means by "This
is normal behavior for the C language, but not PHP. Otherwise, loading
the image into a variable would have required more steps."???

Finally, I tried escaping the data with mysql_real_escape_strings

The string that was inserted into the query was:
5C 30 61 5C 30 62 5C 30 63 5C 30 64

Which is what I expected addcslashes to give..

The data was inserted into the DB correctly.

So this is working as I expected, but why even bother escaping the
nulls if, as shown by the first experiment, it doesn't seem to be
needed?

Having said this, why would you want to escape the other characters:
\n, \r, and \x1a??

Thanks

Taras
Taras,

Just use mysql_real_escape_string(). It's a mysql function which is
made to escape the necessary characters. And it has the added advantage
that it is sensitive to the character set sensitive, so if you ever use
a non-latin1 charset the chars will be handled correctly.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 7 '07 #2
Jerry Stuckle wrote:
Just use mysql_real_escape_string(). It's a mysql function which is
made to escape the necessary characters. And it has the added advantage
that it is sensitive to the character set sensitive, so if you ever use
a non-latin1 charset the chars will be handled correctly.
Though make sure you're using a recent version of MySQL, as older versions
(anything earlier than 4.1.20, plus 5.0-5.0.21) included this bug in
mysql_real_escape:
http://cve.mitre.org/cgi-bin/cvename...=CVE-2006-2753

For better database security, use prepare/execute.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 7 '07 #3
Just use mysql_real_escape_string(). It's a mysql function which is
made to escape the necessary characters. And it has the added advantage
that it is sensitive to the character set sensitive, so if you ever use
a non-latin1 charset the chars will be handled correctly.
I tried that in the experiment (and currently do it in all of my
production code). However, what I don't understand is why using no
escaping at all and mysql_real_escape_string yields the same results.

Also, I read on the manual that you need to set the character set by
using the function 24.2.3.61. mysql_set_character_set(). Is this
correct? Why doesn't SET NAMES just set the required variable as well?
>
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

Feb 8 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Martin Lucas-Smith | last post by:
Can anyone point me to a regular expression in PHP which could be used to check that a proposed (My)SQL database/table/column name is valid, i.e. shouldn't result in an SQL error when created? ...
1
by: leegold2 | last post by:
// This statement below inserting one field works: // mysql_query("INSERT INTO page (page_url) VALUES (\"$url_field\")"); But I wanted to insert into two fields so I was trying all sorts of...
0
by: Lisa | last post by:
I need to apply the HTML formatting tags and the French accented characters in a XML document. The XML is generated from a database that has HTML tags and French accented characters in the records....
8
by: Bill Eldridge | last post by:
I'm trying to grab a document off the Web and toss it into a MySQL database, but I keep running into the various encoding problems with Unicode (that aren't a problem for me with GB2312, BIG 5,...
4
by: Archibald | last post by:
I want to improve security of a multiplayer online game written in php and mysql. Because I'm new to this stuff I would appreciate some tips. If you have time look here...
4
by: Ewok | last post by:
let me just say. it's not by choice but im dealing with a .net web app (top down approach with VB and a MySQL database) sigh..... Anyhow, I've just about got all the kinks worked out but I am...
9
by: Harold Crump | last post by:
Greetings, I have a fairly vanilla PHP web application that stores and retrieves data in a MySQL database. Users will be adding a lot of special characters such as single and double quotes,...
11
by: macca | last post by:
Hi, What should I be using for general MySQL database access? I've been using the traditional mysql extension for ages, but I'm trying to update my style to a more OOP paradigm. I've used...
7
by: wannymahoots | last post by:
optparse seems to be escaping control characters that I pass as arguments on the command line. Is this a bug? Am I missing something? Can this be prevented, or worked around? This behaviour...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.