Update MySQL row with apostrophies in???

Discussion in 'PHP' started by Hade, Mar 31, 2008.

  1. #1
    Hi,
    I'm trying to update a row of text which contains escaped characters (apostrophies)

    For example, with this in the database table:

    I like shopping. it is my hobby.
    Code (markup):
    The following SQL statement works fine:

    update mytable set myfield = 'abc' where myfield = 'I like shopping. it is my hobby.'
    Code (markup):
    Whereas with the following in the database table:

    I like shopping. it\'s my hobby.
    Code (markup):
    The following SQL statement makes no replacement!

    update mytable set myfield = 'abc' where myfield = 'I like shopping. it\'s my hobby.'
    Code (markup):

    Why is this?
    Also, I cannot replace based on the primary key, as the table may not have one! It must be based on the text field!
     
    Hade, Mar 31, 2008 IP
  2. norfstar

    norfstar Peon

    Messages:
    1,154
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I wouldn't be surprised if there was a built in feature to cope with accidentally entered apostrophes, but if there is I do not know of it. A less clean solution that should work however would be:

    update mytable set myfield = 'abc' where myfield LIKE 'I like shopping. it%s my hobby.'
    Code (markup):
     
    norfstar, Mar 31, 2008 IP
  3. Hade

    Hade Active Member

    Messages:
    701
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Thanks, but wouldn't that also match up to the following?:

    'I like shopping. it is ok. and fishing is my hobby.'
    Code (markup):
     
    Hade, Mar 31, 2008 IP
  4. Hade

    Hade Active Member

    Messages:
    701
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    90
    #4
    Sorted it!
    The MySQL software I was using (Navicat) Was hiding the backslashes from me. It turns out I had double-slashed stuff!
     
    Hade, Mar 31, 2008 IP
  5. nation-x

    nation-x Peon

    Messages:
    59
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    god this anti spam crap is annoying...

    php dot net/manual/en/function.mysql-real-escape-string.php <-- the solution is here
     
    nation-x, Mar 31, 2008 IP
  6. Hade

    Hade Active Member

    Messages:
    701
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    90
    #6
    Thanks!
    The problem turned out to be the MySQL automating the escaping!
     
    Hade, Mar 31, 2008 IP