WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Database Discussion Forum This is the place to find help resolving those nagging questions you have about implementing and using all kinds of databases. Need help writing a query? Need an opinion on Oracle? Post here!

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-26-2007, 12:50 PM
kruser kruser is offline
WebProWorld Pro
 

Join Date: Aug 2007
Location: Southern Illinois USA
Posts: 141
kruser RepRank 0
Default single quote in mysql record

What do I need to do to insert a record into a table that has a variable that contains a single quote " ' " ? When the varible contains a string such as "Tom's" ( $variable="Tom's" ) it does not insert into the table for me.

Thanks,
Randy
Reply With Quote
  #2 (permalink)  
Old 08-27-2007, 09:12 AM
DaveSawers's Avatar
DaveSawers DaveSawers is offline
WebProWorld Veteran
 

Join Date: Dec 2006
Location: Calgary, Alberta, Canada
Posts: 332
DaveSawers RepRank 1
Default Re: single quote in mysql record

You have to escape the single quote, so if you're using PHP, something like:

$query = "insert ... var='Tom\'s', ..."

or using your example directly, you could alternatively use:

$variable = "Tom's";
addslashes($variable);

to do the same thing. addslashes is an internal PHP function: PHP: addslashes - Manual
__________________
Dynamic Software Development
www.activeminds.ca
Reply With Quote
  #3 (permalink)  
Old 08-27-2007, 06:20 PM
niggles niggles is offline
WebProWorld Member
 

Join Date: Oct 2005
Posts: 31
niggles RepRank 1
Default Re: single quote in mysql record

Hi,

Instead of addslashes() which means you then have to use stripslashes() when re-displaying it, I prefer to use mysql_real_escape_string() which drops it in without messing around with anything.

Cheers,
Niggles
__________________
-------------------------------------------------
World Music World - bringing the World's Folk Music Cultures Together
http://www.worldmusicworld.com/
-------------------------------------------------
Reply With Quote
  #4 (permalink)  
Old 08-27-2007, 06:36 PM
steveglobal steveglobal is offline
WebProWorld New Member
 

Join Date: Nov 2004
Location: Canada
Posts: 22
steveglobal RepRank 0
Default Re: single quote in mysql record

I always thought that addslashes() was functionally the same as mysql_real_escape_string(), but when niggles posted that, I Googled the subject a bit to find the actual difference.

Turns out that there are some security benefits to mysql_real_escape_string():

Chris Shiflett: addslashes() Versus mysql_real_escape_string()

Although, some have posited that true security comes only from prepared statements:

mysql_real_escape_string() versus Prepared Statements - iBlog - Ilia Alshanetsky
Reply With Quote
  #5 (permalink)  
Old 08-27-2007, 07:13 PM
magnets magnets is offline
WebProWorld New Member
 

Join Date: Aug 2007
Posts: 14
magnets RepRank -1
Default Re: single quote in mysql record

Whenever I receive data from an untrusted source I do a string replace and change a quote (') to a tick (`) character. Even if I forget to change it back later for display purposes, people get the idea anyway.

If it is a trusted source of mine, it does not have a quote (') in the first place.
Reply With Quote
  #6 (permalink)  
Old 08-28-2007, 09:53 AM
kruser kruser is offline
WebProWorld Pro
 

Join Date: Aug 2007
Location: Southern Illinois USA
Posts: 141
kruser RepRank 0
Default Re: single quote in mysql record

Thanks for all the great information, just what I was looking for and more.

Best Regards,
Randy
Reply With Quote
  #7 (permalink)  
Old 08-28-2007, 10:16 PM
boxmonkey boxmonkey is offline
WebProWorld New Member
 

Join Date: Oct 2005
Posts: 12
boxmonkey RepRank 0
Default Re: single quote in mysql record

nggles, you don't have to use strpslashes after usng addslashes unless you're dong t on user nput *and* you have magc quotes turned on. of course f magc quotes s turned on, apostrophes are already nserted correctly and addslashes s not needed (when dealng wth user data)
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Tags: mysql, quote, record, single



Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for quote about Google PR... kissels Google Discussion Forum 6 05-04-2006 12:43 PM
Quote of the ------------------------------> Day 5th Jan JuniorOnline Breakroom (General: Any Topic) 1 01-05-2006 03:17 AM
Quote of the day archive rosie WebProWorld: Guidelines/Announcements/Suggestions 1 11-01-2005 04:03 PM
Find-A-Quote panther Submit Your Logo For Review 5 08-13-2005 05:14 AM
Can I quote you on that? :) wenwilder Breakroom (General: Any Topic) 8 11-21-2003 12:57 AM


Search Engine Friendly URLs by vBSEO 3.0.0