The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. February 12, 2012, 04:30:49 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Best Way to Escape Content for Insertion to MySQL  (Read 1471 times)
KaptainKrayola
Keeper of Pie
Global Moderator
Lifer
*****
Offline Offline

Posts: 994



View Profile WWW
« on: November 19, 2009, 03:30:07 PM »

So, the Kaptain here has been running into issues with inserting big blocks of syndicated text into his DB where sometimes it will just fail with some kind of goofy error saying there is an issue with the syntax of my query (which there totally isn't cause my queries are teh win).  So, I'm wondering what the best method(s) for escaping and making big blocks of text mySQL friendly for insertion into the table.

Discuss in 3, 2, 1...GO!
Logged

We can't stop here, this is bat country.
lamontagne
Journeyman
***
Offline Offline

Posts: 89


View Profile
« Reply #1 on: November 19, 2009, 04:31:24 PM »

Load data infile or properly escaped batch inserts. That being said I find it easier to store huge chunks of text in a file and store the path plus only what is needed in the db
Logged

"Long time no see. I only pray the caliber of your questions has improved." - Kevin Smith
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 9792



View Profile
« Reply #2 on: November 19, 2009, 04:47:53 PM »

Am I missing it here? $newbuff = mysql_escape_string($oldBuff); ...?

you've probably got some double or single quotes in it.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5604


Back in my day we had 9 planets


View Profile
« Reply #3 on: November 19, 2009, 04:50:29 PM »

I will again recommend my old standby.

base64 encode it
OR
hex encode it

Although there are function out there built into assorted languages that are supposedly able to escape any mess of shit you throw at it, I have found them all to be PHAIL. I gave up eons ago, though I keep trying them out.

hex or b64 are ZERO danger to your database, no matter what.

Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
KaptainKrayola
Keeper of Pie
Global Moderator
Lifer
*****
Offline Offline

Posts: 994



View Profile WWW
« Reply #4 on: November 19, 2009, 07:07:56 PM »

Am I missing it here? $newbuff = mysql_escape_string($oldBuff); ...?

you've probably got some double or single quotes in it.

been there, done that - still had issues.  I had thought that was the way to go but still run into the odd insert that fails.  Tried a variety of other things as well but still can't get it to 100% success rate so i thought i may be missing something.

@nuts - clever idea with hex or base64 i may have to give that a whirl.
Logged

We can't stop here, this is bat country.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5604


Back in my day we had 9 planets


View Profile
« Reply #5 on: November 19, 2009, 08:47:38 PM »

between smart slashes smart quotes smart smartyness and all that escape shit, and it being different on every damn system, Im surprised mysql ever works with text being inserted.

I think another option would be binary, but I never have bothered to try it.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
oldenstylehats
Rookie
**
Offline Offline

Posts: 19


View Profile
« Reply #6 on: November 19, 2009, 08:59:57 PM »

I will again recommend my old standby.

base64 encode it
OR
hex encode it

Although there are function out there built into assorted languages that are supposedly able to escape any mess of shit you throw at it, I have found them all to be PHAIL. I gave up eons ago, though I keep trying them out.

hex or b64 are ZERO danger to your database, no matter what.


Thank you. I'd never thought of that before. Great idea.
Logged

No links in signatures please
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5604


Back in my day we had 9 planets


View Profile
« Reply #7 on: November 19, 2009, 10:03:12 PM »

Its a  D'oh!
for most folks when they hear it.

the hard part is remembering to do it... lol
I always forget.

downside is searchability. You cant really do LIKE queries or FULLTEXT. But generally speaking, it would only be used for BIGASSTEXT or dirty text. Shorter text tends to have less probability of some magic quote combo that breaks the escape functions.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 9792



View Profile
« Reply #8 on: November 19, 2009, 11:05:14 PM »

It's for that very reason that, although I like the concept, I don't like it for pure text content.

Kaptain have you thought about doing a straight removal of all non-ascii chars, then dump backslashes, then escape it? That should just about do it. If that doesn't, PM me a SQL that fails. I think it's simpler than this.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
arms
Expert
****
Offline Offline

Posts: 235



View Profile
« Reply #9 on: November 20, 2009, 01:34:53 PM »

i know i have had headaches from character encoding problems when pulling shit from the web.
documents are not always using the character set they claim to be.
at least make sure the columns you are inserting into are utf and not latin.
Logged
KaptainKrayola
Keeper of Pie
Global Moderator
Lifer
*****
Offline Offline

Posts: 994



View Profile WWW
« Reply #10 on: November 20, 2009, 03:34:09 PM »

thanks for the suggestions all - i'm going to hammer on it some and see if there is anything i have missed.

 
Logged

We can't stop here, this is bat country.
herbacious
Journeyman
***
Offline Offline

Posts: 51


View Profile
« Reply #11 on: December 16, 2009, 08:39:48 AM »

prepared statements any use?
Logged
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!