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.
Printable View
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.
Before saving you may try to replace those single quotes into two single quotes....
VB Code:
Public Function CleanText(ByVal pString As String) As String CleanText = Replace$(pString, "'", "''") End Function
Oooppsss... I just realized this is in PHP... :blush:
At least you tried ;)
Have you tried replacing the single quote with PHP code before saving?
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)
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...
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.
have you tried usingwhich returns the ' character?Code:chr(39)
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
You do not need to use the stripslashes function when retrieving the data.