Results 1 to 10 of 10

Thread: Save "'" in MySQL database

  1. #1

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Save "'" in MySQL database

    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.

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Save "'" in MySQL database

    Before saving you may try to replace those single quotes into two single quotes....

    VB Code:
    1. Public Function CleanText(ByVal pString As String) As String
    2.     CleanText = Replace$(pString, "'", "''")
    3. End Function

    Oooppsss... I just realized this is in PHP...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Save "'" in MySQL database

    At least you tried

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Save "'" in MySQL database

    Have you tried replacing the single quote with PHP code before saving?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Save "'" in MySQL database

    I could replace each ' with "" before saving but then I wouldn't know which name contains a ' and which ones don't (eg O'Brien)

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Save "'" in MySQL database

    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...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Save "'" in MySQL database

    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.

  8. #8
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Save "'" in MySQL database

    have you tried using
    Code:
    chr(39)
    which returns the ' character?
    Chris

  9. #9
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    Re: Save "'" in MySQL database

    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

  10. #10
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Save "'" in MySQL database

    You do not need to use the stripslashes function when retrieving the data.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width