PDA

Click to See Complete Forum and Search --> : Save "'" in MySQL database


lintz
Mar 20th, 2006, 11:39 PM
I have names like O'Brien and O'Keefe that I need to save in my MySQL database but it throws up an error because of the "'".

How can I over come this as I don't want to save name like OBrien and OKeefe?

Thanks.

dee-u
Mar 21st, 2006, 12:05 AM
Before saving you may try to replace those single quotes into two single quotes....

Public Function CleanText(ByVal pString As String) As String
CleanText = Replace$(pString, "'", "''")
End Function

Oooppsss... I just realized this is in PHP... :blush:

lintz
Mar 21st, 2006, 01:21 AM
At least you tried ;)

dee-u
Mar 21st, 2006, 01:22 AM
Have you tried replacing the single quote with PHP code before saving?

lintz
Mar 21st, 2006, 01:40 AM
I could replace each ' with "" before saving but then I wouldn't know which name contains a ' and which ones don't :confused: (eg O'Brien)

dee-u
Mar 21st, 2006, 01:44 AM
In VB6.0 I use to use the function I posted earlier in every string I'll save to the database, I don't know any PHP but perhaps you could use a parameterized query, that way it would be safer against sql injections also...

penagate
Mar 21st, 2006, 02:20 AM
Backslash escape it

\'

If you don't have magic quotes on (which it is by default) then you can use addslashes() to escape an input string for you.

the182guy
Mar 21st, 2006, 02:22 AM
have you tried using chr(39) which returns the ' character?

AIS4U
Mar 21st, 2006, 02:23 AM
lintz:

You have to escape the single quote by putting a backslash in front of it.
O\'Brien.

You will also need to use the MySQL functions AddSlashes and StripSlashes when storing (AddSlashes) and retrieving (StripSlashes) the data into and out of your MySQL database.

I would suggest you check out the online MySQL Manual at: http://dev.mysql.com/doc/refman/5.0/en/

Good Luck

visualAd
Mar 21st, 2006, 02:35 AM
You do not need to use the stripslashes function when retrieving the data.