PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old July 23rd, 2004, 02:55 PM
daidalus13 daidalus13 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 16 daidalus13 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 12 m 51 sec
Reputation Power: 0
Escaping special characters for storage in database (MySQL)

hello all,
I am inserting data (text) to a MySQL database. I need to escape database control characters before I actually send the SQL statement to the database.



Some questions:



1.

If I use addslashes(), I do my job but… do I have to use stripslashes() when I retrieve my data from the database? Using the MySQL command line interface, I access my database and the record in question simply has no added slashes. So, I assume that there is no need to use stripslashes() when taking data out of the database. What do you think?

( I have magic_quotes_gpc set to off)



2.

Another question arises when it comes to storing binary data in a blob column of a table in my MySQL database. I read somewhere that using addslashes() can corrupt the data and the safest way when storing binary data is to use the base64_encode() function. I read about it and I am now familiar with what it does. I also know that the size of the encoded file can be 33% larger than the original. I am prepared for that. What do you think?



3.

How about using the mysql_real_escape_string() instead of addslashes?



Thanks.
daidalus13

Reply With Quote
  #2  
Old July 23rd, 2004, 05:31 PM
pentapenguin pentapenguin is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 51 pentapenguin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 7 sec
Reputation Power: 6
Quote:
Originally Posted by daidalus13
If I use addslashes(), I do my job but… do I have to use stripslashes() when I retrieve my data from the database? Using the MySQL command line interface, I access my database and the record in question simply has no added slashes. So, I assume that there is no need to use stripslashes() when taking data out of the database. What do you think?
Yes, addslashes will work.
MySQL uses quotes when entering data so extra quotes will break the query.
As you oberved, there's no need to use stripslashes when retrieving data.

Quote:
Originally Posted by daidalus13
2. Another question arises when it comes to storing binary data in a blob column of a table in my MySQL database. I read somewhere that using addslashes() can corrupt the data and the safest way when storing binary data is to use the base64_encode() function. I read about it and I am now familiar with what it does. I also know that the size of the encoded file can be 33% larger than the original. I am prepared for that. What do you think?
I've always heard you should do the base64_encode() but I may be wrong.

Quote:
Originally Posted by daidalus13
How about using the mysql_real_escape_string() instead of addslashes?
Either one will work.
It's largely a matter of preference though it's recommened that you use the mysql_real_escape_string.
Hope this helps!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Escaping special characters for storage in database (MySQL)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway